使用powermock

  • 2016-08-17
  • J2EE

在pom.xml加入

        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
            <version>3.3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.6.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-easymock</artifactId>
            <version>1.6.5</version>
            <scope>test</scope>
        </dependency>

如果报错,有可能项目中包含有低版本的javassist

java.lang.NoSuchMethodError: javassist.CtMethod.hasAnnotation(Ljava/lang/Class;)Z

尝试加入新版本的javassist,注意classpath的优先级是否正确

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.19.0-GA</version>
            <scope>test</scope>
        </dependency>

使用文档可以看官网:https://github.com/jayway/powermock




fatkun

折腾一下又不会死~