使用powermock

在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

updatedupdated2024-08-302024-08-30