Junit + Mockito 使用资料整理
1、简单介绍
junit,验证实际调用结果,可以断点调试,也可以检查代码覆盖率
mockito,mock测试框架的一种,可以用来mock外部接口服务和数据,并非真实调用,辅助完成数据mock来验证流程
2、学习资料
Mockito官网
https://site.mockito.org
B站汪文君Mockito视频讲解
https://www.bilibili.com/video/av67760177
3、环境配置
版本号可以自己适配
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<!-- mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
4、测试代码结构及实例
基础测试类,通过注解配置Junit+Mockito测试环境
/**
* created by guanjian on 2019/11/4 10:54
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-config.xml")
public class BaseTest {
@BeforeClass
public static void init() {
}
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
}
具体测试类
@InjectMocks 真实执行,针对实现类使用,不能作用在接口上
@Mock 非真实执行,针对Mock类进行虚拟
@Spy 真实执行
/**
* created by guanjian on 2019/11/4 9:20
*/
public class ChsiServiceTest extends BaseTest {
private final static Logger LOGGER = LoggerFactory.getLogger(ChsiServiceTest.class);
@InjectMocks
private ChsiServiceImpl chsiService;
@Mock
private CampusVerifyFlowResource campusVerifyFlowResourceJSF;
@Mock
private H5CampusStudentAuthResource h5CampusStudentAuthResourceJSF;
@Mock
private BizCacheService bizCacheService;
@Spy
private ChsiAuthResultVo mqVo;
@Before
public void initMqVo() {
mqVo.setZxs("1");
mqVo.setReqId(UUID.randomUUID().toString());
mqVo.setAuth("1");
}
@Test
public void test_handleChsiAuthResult() {
String pin = "testPin";
StudentActivationRes studentActivationResSucc = new StudentActivationRes();
com.jd.jr.baitiao.campus.export.vo.result.Result resultSucc = new com.jd.jr.baitiao.campus.export.vo.result.Result();
resultSucc.setCode("CSA_SA_00");
studentActivationResSucc.setResult(resultSucc);
StudentActivationRes studentActivationResFail = new StudentActivationRes();
com.jd.jr.baitiao.campus.export.vo.result.Result resultFail = new com.jd.jr.baitiao.campus.export.vo.result.Result();
resultFail.setCode("CSA_SA_01");
studentActivationResFail.setResult(resultFail);
when(bizCacheService.getChsiPinByReqId(anyString())).thenReturn(pin, pin, pin);
when(campusVerifyFlowResourceJSF.updateCampusVerifyFlow(any(CampusVerifyFlowUpdateReq.class))).thenReturn(Result.buildSuccessResult(), Result.buildErrorResult("0001", "失败"), Result.buildSuccessResult());
when(h5CampusStudentAuthResourceJSF.studentActivation(any(StudentActivationReq.class))).thenReturn(studentActivationResSucc, studentActivationResFail, studentActivationResSucc);
doNothing().when(bizCacheService).delChsiPinReqId(pin);
//正常流程
LOGGER.info("结果MQ处理开始。mqVo:{}", GsonUtils.toJson(mqVo));
chsiService.handleChsiAuthResult(mqVo);
LOGGER.info("结果MQ处理结束。");
//流水失败
LOGGER.info("结果MQ处理开始。mqVo:{}", GsonUtils.toJson(mqVo));
try {
chsiService.handleChsiAuthResult(mqVo);
} catch (Exception e) {
Assert.assertThat("更新流水失败", e, instanceOf(IllegalArgumentException.class));
}
LOGGER.info("结果MQ处理结束。");
//激活学生身份失败
LOGGER.info("结果MQ处理开始。mqVo:{}", GsonUtils.toJson(mqVo));
try {
chsiService.handleChsiAuthResult(mqVo);
} catch (Exception e) {
Assert.assertThat("激活失败", e, instanceOf(IllegalArgumentException.class));
}
LOGGER.info("MQ处理结束。");
}
/**
* 验证mq
*
* @description [auth、reqId]不为空。
* [auth]必须是0或1或3。
* [zxs]必须是0或1或空。
*/
@Test
public void test_checkMessageBody() {
//[auth、reqId]不为空。
ChsiAuthResultVo vo = new ChsiAuthResultVo();
vo.setAuth(null);
vo.setReqId(UUID.randomUUID().toString());
try {
LOGGER.info("请求开始。req:{}", GsonUtils.toJson(vo));
chsiService.handleChsiAuthResult(vo);
} catch (Exception e) {
assertThat(e, instanceOf(IllegalArgumentException.class));
} finally {
vo = new ChsiAuthResultVo();
}
vo.setAuth("1");
vo.setReqId(null);
try {
LOGGER.info("请求开始。req:{}", GsonUtils.toJson(vo));
chsiService.handleChsiAuthResult(vo);
} catch (Exception e) {
assertThat(e, instanceOf(IllegalArgumentException.class));
}
vo.setAuth(null);
vo.setReqId(null);
try {
LOGGER.info("请求开始。req:{}", GsonUtils.toJson(vo));
chsiService.handleChsiAuthResult(vo);
} catch (Exception e) {
assertThat(e, instanceOf(IllegalArgumentException.class));
}
vo.setAuth("1");
vo.setReqId(UUID.randomUUID().toString());
try {
LOGGER.info("请求开始。req:{}", GsonUtils.toJson(vo));
chsiService.handleChsiAuthResult(vo);
} catch (Exception e) {
assertThat(e, instanceOf(IllegalArgumentException.class));
}
//[auth]必须是0或1或3。
vo.setAuth("5");
vo.setReqId(UUID.randomUUID().toString());
vo.setZxs(null);
try {
LOGGER.info("请求开始。req:{}", GsonUtils.toJson(vo));
chsiService.handleChsiAuthResult(vo);
} catch (Exception e) {
assertThat(e, instanceOf(IllegalArgumentException.class));
}
//[zxs]必须是0或1或空。
vo.setAuth("0");
vo.setReqId(UUID.randomUUID().toString());
vo.setZxs("5");
try {
LOGGER.info("请求开始。req:{}", GsonUtils.toJson(vo));
chsiService.handleChsiAuthResult(vo);
} catch (Exception e) {
assertThat(e, instanceOf(IllegalArgumentException.class));
}
}
}
5、测试用例代码覆盖率
1、首先找到IDEA工具中Edit Configuration选项
2、点击小加号,选择Junit
3、配置Junit中的Configuration基础配置,定位要运行的项目,参考下图,选择项目路径
4、配置Junit中的Code Coverage,按照包名去匹配检查覆盖率,参考下图,我们要检查web和service包下代码覆盖率
5、以上步骤配置完成Junit和检查规则,切回到主视图,选择配置的Junit,点击下图运行按钮,开始跑测试用例
6、运行完成后,可以在控制台看到检查结果,运行的测试方法数,成功数,失败数。
7、IDEA右侧视图会显示我们的Junit配置检查规则下代码覆盖率情况,有三个维度,类、方法、行,我们关注方法这栏即可,web覆盖100%,service覆盖60%。点击进入可以看到每个包目录下更细致的覆盖情况,一般像缓存方法的api我们可以忽略,mock方法执行的不会实际执行会影响覆盖率,执行失败的方法也会影响覆盖率。