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-JUnit-1.6.2.zip 是一个包含PowerMock、Mockito和JUnit集成库的压缩包,适用于Java开发环境。这个版本的PowerMock要求使用1.6或更高版本的Java Development Kit (JDK)。下面将详细解释这三个主要...
powermock-mockito-demo with springboot 使用在springboot项目中使用powermock-mockito进行单元测试,演示mock static、private、whenNew、exception的测试。 注意 示例使用spring-boot 2.0.3.RELEASE 它间接引用 ...
PowerMock和Mockito是两个非常流行的Java单元测试框架,结合JUnit,它们可以提供强大的测试能力。在本文中,我们将深入探讨"PowerMock+Mockito-junit测试套件1.6.3版本"中的关键概念、功能和使用方法。 **PowerMock...
PowerMock-Mockito-JUnit-1.6.3.zip 是一个包含最新版本的 PowerMock、Mockito 和 JUnit 库的压缩文件。这些工具在Java开发中被广泛用于单元测试和模拟,尤其对于处理静态方法、构造器、final类和方法、私有方法等...
赠送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是一个强大的Java单元测试框架,它扩展了如Mockito这样的工具,允许开发者模拟静态方法、构造函数、final类和方法、私有方法以及删除静态初始化器。在Java开发中,有时我们需要对不可Mock的对象进行单元...
使用 mockito-core 而不是 mockito-all 的 PowerMock 示例。 问题(参见 pom.xml): a) 如果 JUnit 在 powermock-api-mockito 之前是显式依赖项,则 JUnit 的 Hamcrest 1.3 依赖项会获胜并且测试运行良好。 b) ...
"MR Unit test需要的相关jar包"指的是为了有效地对MapReduce程序进行单元测试所必需的库文件。这些jar包通常不包括Hadoop运行环境的基础jar包,因为它们专注于测试MapReduce任务本身,而不是整个Hadoop生态系统。...
java运行依赖jar包
Spring Boot项目通常使用JUnit 5,所以我们可以在测试类上使用`@RunWith(SpringRunner.class)`注解,同时使用`@SpringBootTest`注解来加载整个Spring应用上下文。然而,为了进行单元测试,我们可能只关心单个服务或...
java运行依赖jar包
《PowerMock实战手册》是一本专注于使用PowerMock进行单元测试的指南,结合了Junit测试框架和Mockito库,为开发者提供了全面的测试解决方案。在实际的软件开发中,单元测试是确保代码质量的重要环节,它能帮助我们找...
1. 引入PowerMock依赖:在项目构建文件(如Maven的pom.xml或Gradle的build.gradle)中添加PowerMock和Mockito的相关依赖。 2. 准备测试类:使用`@RunWith(PowerMockRunner.class)`注解标记测试类,并使用`@...
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 是一个强大的Java单元测试框架,它扩展了其它单元测试工具,如Mockito,使得开发者可以模拟静态方法、构造函数、final类和方法、删除静态初始化器等。这款库在进行复杂对象模拟时非常有用,尤其是在处理...
PowerMock 也是一个单元测试模拟...PowerMock 的目的就是在当前已经被大家所熟悉的接口上通过添加极少的方法和注释来实现额外的功能,目前,PowerMock 仅支持 EasyMock 和 Mockito。 标签:PowerMock 单元测试
java运行依赖jar包
5. **使用PowerMock进行单元测试**:在你的测试类中,你需要使用`@RunWith(PowerMockRunner.class)`注解来运行测试,并使用`@PrepareForTest`注解来指定要模拟的类。然后,你可以使用Mockito的API来模拟这些类的行为...