工具
我将在下面介绍下PowerMockRunner和SpringRunner两个单元测试的运行环境。
SpringRunner
SpringRunner 继承了SpringJUnit4ClassRunner,SpringRunner是SpringJUnit4ClassRunner的一个别名,没有扩展任何功能。下面我们来看下示例
package com.hly.unitest.controller;
import com.alibaba.fastjson.JSON;
import com.hly.unitest.entity.UserInfo;
import com.hly.unitest.service.UserService;
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 UserServiceNoMockTest {
@Autowired
private UserService userService;
@Test
public void getUser() throws Exception {
UserInfo result = userService.getUser("6f49aa9e-1afc-4439-ad21-25ae90dde566");
System.out.println("the 1st time: result = " + JSON.toJSONString(result));
}
}
分析:
首先我们分析下注解:
@RunWith: 用于指定junit运行环境,是junit提供给其他框架测试环境接口扩展,为了便于使用spring的依赖注入,spring提供了org.springframework.test.context.junit4.SpringJUnit4ClassRunner作为JUnit测试环境。在JUnit中有很多个Runner,他们负责调用你的测试代码,每一个Runner都有各自的特殊功能,你要根据需要选择不同的Runner来运行你的测试代码。我们此篇文章只探讨SpringRunner和PowerMockRunner。
@SpringBootTest: 替代了spring-test中的@ContextConfiguration注解,目的是加载ApplicationContext,启动spring容器, @SpringBootTest注解会自动检索程序的配置文件,检索顺序是从当前包开始,逐级向上查找被@SpringBootApplication或@SpringBootConfiguration注解的类。
提出问题: 这样的测试方式会连接数据库,进行网络请求等等耗时操作,甚至数据库中的准备数据是个非常麻烦的事情,各个逻辑分支如果都需要测试,这在大型的项目中工作量是不可想象的,所以有没有可能对数据库这种操作直接不处理,这需要mock能力,为此引入了MockBean,下面来看示例:
[
](https://blog.csdn.net/rajayu/article/details/103567036)
package com.hly.unitest.controller;
import com.alibaba.fastjson.JSON;
import com.hly.unitest.dao.UserInfoMapper;
import com.hly.unitest.entity.UserInfo;
import com.hly.unitest.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import static org.mockito.ArgumentMatchers.any;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService;
@MockBean
private UserInfoMapper userInfoMapper;
@Test
public void getUser() {
UserInfo result = userService.getUser("6f49aa9e-1afc-4439-ad21-25ae90dde566");
System.out.println("the 1st time: result = " + JSON.toJSONString(result));
UserInfo userInfo = mockUserInfo();
Mockito.when(userInfoMapper.selectByUserId(any())).thenReturn(userInfo);
result = userService.getUser("6f49aa9e-1afc-4439-ad21-25ae90dde566");
System.out.println("the 2nd time: result = " + JSON.toJSONString(result));
}
private UserInfo mockUserInfo() {
UserInfo userInfo = new UserInfo();
userInfo.setUserId("654321");
userInfo.setUserName("B");
userInfo.setPassword("pwB");
userInfo.setGender("女");
return userInfo;
}
}
分析:
我们在UserInfoMapper上加了注解@MockBean,这个注解的意思所有的UserInfoMapper的方法都被mock,返回都将变成null,所以第一次返回值为null;我们在测试方法中借助Mockito.when将userInfoMapper.selectByUserId的返回值进行自定义,这样就完成了对数据库的返回对象的控制,不用怕数据库的更改导致测试用例的分支没有跑完成的问题出现。
提出问题: 我们发现这样的处理虽然符合了上个问题的预期,但是又有了新的问题出现,第一:既然将数据库和网络操作全部mock,那好像就没有必要启动Spring,@MockBean有个特别大的危害就是导致频繁的启动Spring,而spring boot 的启动时间比较耗时,所以@MockBean,很有可能导致测试运行的很慢;第二:如果有这样一个方法,是UserService 的私有方法,里面特别复杂的操作,我并不想在这个测试用例中进行测试,可以不可以mock,或者有一个静态类,类中的方法有没有方式mock,还有UserService的变量我又没有方式可以直接赋值等等,这些SpringRunner也就是SpringJUnit4ClassRunner都是没有办法解决的,这需要下面的有个JUNIT工具解决,也就是PowerMockRunner。
[
](https://blog.csdn.net/rajayu/article/details/103567036)
PowerMockRunner
PowerMock基本上cover了所有Mockito不能支持的case(大多数情况也就是静态方法,但其实也可以支持私有方法和构造函数的调用)。PowerMock使用了字节码操作,因此它是自带Junit runner的。在使用PowerMock时,必须使用@PrepareForTest注释被测类,mock才会被执行。下面我们来看示例:
对UserService进行单元测试:
package com.hly.unitest.service;
import com.hly.unitest.dao.UserInfoMapper;
import com.hly.unitest.entity.UserInfo;
import com.hly.unitest.model.UserInfoRequest;
import com.hly.unitest.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class UserService {
private String finalStr = "finalStr";
@Autowired
private UserInfoMapper userInfoMapper;
public void save(UserInfoRequest request) {
UserInfo userInfo = new UserInfo();
userInfo.setUserId(request.getUserId());
userInfo.setUserName(request.getUserName());
userInfo.setPassword(request.getPassword());
userInfo.setGender(request.getGender());
Date currDate = CommonUtil.getMaxDayOfThisMonth(new Date());
userInfo.setCreateTime(currDate);
userInfo.setUpdateTime(currDate);
userInfoMapper.insertSelective(userInfo);
}
public UserInfo getUserInfo(String userId) {
if (CommonUtil.isEmpty(userId)) {
return null;
}
String assetStr = getAssetStr0("a", "b");
if (!"un".equals(assetStr)){
System.out.println("assetStr = " + assetStr);
return null;
}
getAssetNull();
UserInfo userInfo = userInfoMapper.selectByUserId(userId);
return userInfo;
}
public UserInfo getUser(String userId) {
UserInfo userInfo = userInfoMapper.selectByUserId(userId);
return userInfo;
}
public String testStaticMethod() {
String randomStr = CommonUtil.generateUUID();
return randomStr;
}
public String testClassVariables() {
return finalStr;
}
private String getAssetStr() {
System.out.println("getAssetStr");
return "unit-test";
}
private String getAssetStr0(String a, String b) {
System.out.println("getAssetStr0");
return "unit-test";
}
private void getAssetNull() {
System.out.println("getAssetNull");
return;
}
}
package com.hly.unitest.controller;
import com.alibaba.fastjson.JSON;
import com.hly.unitest.dao.UserInfoMapper;
import com.hly.unitest.entity.UserInfo;
import com.hly.unitest.service.UserService;
import com.hly.unitest.util.CommonUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.springframework.test.util.ReflectionTestUtils;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserService.class, CommonUtil.class})
public class UserServicePowerRunnerTest {
@InjectMocks
private UserService userService;
@Mock
private UserInfoMapper userInfoMapper;
@Before
public void setUp() {
// mock私有方法
userService = PowerMockito.spy(new UserService());
ReflectionTestUtils.setField(userService, "userInfoMapper", userInfoMapper);
}
@Test
public void getUserInfo() throws Exception {
UserInfo userInfo = mockUserInfo();
when(userInfoMapper.selectByUserId(any())).thenReturn(userInfo);
// 控制私有方法的返回值
PowerMockito.doReturn("un").when(userService, "getAssetStr0", any(), any());
UserInfo result = userService.getUserInfo("6f49aa9e-1afc-4439-ad21-25ae90dde566");
System.out.println(JSON.toJSONString(result));
}
@Test
public void testStaticMethod() throws Exception {
String result = userService.testStaticMethod();
System.out.println("the 1st time: result = " + result);
// mock静态方法
PowerMockito.mockStatic(CommonUtil.class);
String str = "123";
when(CommonUtil.generateUUID()).thenReturn(str);
result = userService.testStaticMethod();
System.out.println("the 2nd time: result = " + result);
}
@Test
public void testClassVariables() throws Exception {
String result = userService.testClassVariables();
System.out.println("the 1st time: result = " + result);
// mock类变量
Whitebox.setInternalState(userService, "finalStr", "FINAL-STR");
result = userService.testClassVariables();
System.out.println("the 2nd time: result = " + result);
}
private UserInfo mockUserInfo() {
UserInfo userInfo = new UserInfo();
userInfo.setUserId("654321");
userInfo.setUserName("B");
userInfo.setPassword("pwB");
userInfo.setGender("女");
return userInfo;
}
}
https://blog.csdn.net/rajayu/article/details/103567036
https://www.cnblogs.com/AdaiCoffee/p/10700097.html