Build Spring Unit test by using PowerMock with Mockito

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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值