sometimes we need to test final class or static class, at this time we need powermock.
1, update pom.xml to import powermock
<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <scope>test</scope> </dependency>
2. then we can use powermock in unit test like below:
in this example, ReadInputRegistersResponse is a final class.
@RunWith(PowerMockRunner.class) @PrepareForTest(ReadInputRegistersResponse.class) public class InverterDetailsResponseTest { private ReadInputRegistersResponse mockModbusResp; @test public void test1() { mockModbusResp = mock(ReadInputRegistersResponse.class); // ...... }
but sometimes in unit test, we need have @RunWith(SpringJUnit4ClassRunner.class), when we still need powermock, we will avoid @RunWith(PowerMockRunner.class).
below is the example how to avoid @RunWith(PowerMockRunner.class), but still use powermock.
1. it is simple , just add : PowerMockAgent.initializeIfNeeded() method.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/inverter-persistent-config.xml"}) public class TcpModbusServiceTest extends AbstractInverterTest { static { PowerMockAgent.initializeIfNeeded(); } }
Note:
at this time the mocked class should be a local variable like this:
private void mockModbusResponse() throws CommunicationException { ReadInputRegistersResponse mockModbusResp = mock(ReadInputRegistersResponse.class); // ... ... ... }
2. if you are using maven and want to run test by mvn test, then you need to add below lines in build config as well:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4</version> <configuration> <excludes> <exclude>**/*IT.java</exclude> <exclude>**/Abstract*.java</exclude> </excludes> <argLine> -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar </argLine> <useSystemClassloader>true</useSystemClassloader> </configuration> </plugin> </plugins> </build>
3. in pom.xml dependency is a bit different:
<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4-rule-agent</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock.tests</groupId> <artifactId>powermock-tests-utils</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <scope>test</scope> </dependency>
that's it.
相关推荐
powermock-mockito-demo with springboot 使用在springboot项目中使用powermock-mockito进行单元测试,演示mock static、private、whenNew、exception的测试。 注意 示例使用spring-boot 2.0.3.RELEASE 它间接引用 ...
PowerMock+Mockito-junit测试套件1.6.3版本
powermock-mockito-junit-1.6.3.zip 当前最新版,请使用1.6以上版本jdk。 如果抛出 Bad version in .class file,请检查jdk版本是否太低
赠送jar包:powermock-api-mockito2-2.0.9.jar; 赠送原API文档:powermock-api-mockito2-2.0.9-javadoc.jar; 赠送源代码:powermock-api-mockito2-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-api-...
powermock 版本要求比较严格,请使用1.6及以上jdk
使用 mockito-core 而不是 mockito-all 的 PowerMock 示例。 问题(参见 pom.xml): a) 如果 JUnit 在 powermock-api-mockito 之前是显式依赖项,则 JUnit 的 Hamcrest 1.3 依赖项会获胜并且测试运行良好。 b) ...
PowerMock实战手册。采用的是Junit+PowerMock+Mockito这样的组合来进行讲述。
java运行依赖jar包
PowerMock比Mockito强大,能模拟static,private和final的方法,还能模拟构造方法。
java运行依赖jar包
Mockito is a testing framework that allows you to easily create test doubles (mocks) ... This book shows the use of Mockito's popular unit testing frameworks such as JUnit, PowerMock, TestNG, and so on.
powermock powermock powermock powermock
By using a custom classloader no changes need to be done to the IDE or continuous integration servers which simplifies adoption. Developers familiar with the supported mock frameworks will find ...
Instant Mock Testing with PowerMock 7 Saying Hello World! (Simple) 8 Getting and installing PowerMock (Simple) 14 Mocking static methods (Simple) 22 Verifying method invocation (Simple) 28 Mocking ...
PowerMock 也是一个单元测试模拟...PowerMock 的目的就是在当前已经被大家所熟悉的接口上通过添加极少的方法和注释来实现额外的功能,目前,PowerMock 仅支持 EasyMock 和 Mockito。 标签:PowerMock 单元测试
java运行依赖jar包
tesunit junit easymock PowerMock
java单元测试 junit powermock mockito ,下载直接引用就行了。
Powermock是基于现有的mock框架,easymock和mockito而建立的一个非常强大的框架,它具备easymock和mockito所不具备的一些功能,该系列教材详细的说明了如何使用powermock的所有特性
全面介绍PowerMock的使用,帮助初学者彻底了解PowerMock的使用,提高代码的健壮性