SpringBoot——使用powermock进行静态方法的单元测试
关注微信公众号:CodingTechWork,一起学习进步。
引言
xxxService
类引用了xxxUtils
类的xxxstaticMethod()
方法,当对xxxService
类进行单元测试的时候,如何进行呢?本文介绍了,使用powermock
进行静态方法的mock。
service层单测
无静态方法引用
依赖
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
service层类
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String aMethod(String key) {
... ...
}
}
service层测试类
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoServiceImplTest{
@Autowired
private DemoService demoService;
@Test
public void testAMethod() {
String key = "xxx";
String res = "yyy";
Assert.assertEquals(demoService.aMethod(key), res);
}
}
有静态方法引用
依赖
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
</dependency>
service层类
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String aMethod(String key) {
... ...
try {
String res0 = XXXUtils.staticBMethod(key);
... ...
} catch (IOException) {
... ...
}
... ...
}
}
service层测试类
import xxx.xxx.XXXUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDeletegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@PrepareForTest(XXXUtil.class)
@PowerMockIgnore("javax.management.*")
public class DemoServiceImplTest{
@Autowired
private DemoService demoService;
@Test
public void testAMethodWithoutException() {
String key = "xxx";
String res0 = "yyy";
String res = "zzz";
PowerMockitio.mockStatic(XXXUtil.class);
PowerMockito.when(XXXUtil.staticBMethod(Mockito.anyString())).thenReturn(res0);
Assert.assertEquals(demoService.aMethod(key), res);
}
@Test
public void testAMethodWithException() {
String key = "xxx";
String res0 = "yyy";
String res = "zzz";
PowerMockitio.mockStatic(XXXUtil.class);
PowerMockito.when(XXXUtil.staticBMethod(Mockito.anyString())).thenThrow(IOException.class);
Assert.assertEquals(demoService.aMethod(key), res);
}
}
烧不死的鸟就是凤凰