大家好,又见面了,我是你们的朋友全栈君。
1. 概述:
小伙伴们,想必你看到此文,估计也是被公司强制要求的单元测试覆盖率苦恼了,我想说:“me,too”。
鄙人不才,之前写单元测试,都是环境和上下文依赖的,其实算不上是Junit测试,有时候甚至在自己的类中写一个main方法,进行简单的测试,这些对于菜鸟和新手都是必须经历的过程,如果自己开发,我仍然会这样测试,但是对于公司来说,公司的Sonar或其他工具很难评价项目的代码质量,比如测试覆盖率等,没有覆盖的代码,就意味着风险,特别是对于质量就是生命的企业来说,更是如此。
对于一个项目,选择什么样的单元测试Mock框架十分重要,因为如果你要测试的方法,框架不支持那就麻烦了,只能望“码”兴叹了!
废话少说,现在给大家推荐JMockit, 官网: http://jmockit.org/ 。以下图为例,大家就应该知道JMockit的强大功能了。
然并卵,说人话,你仍然不知道该怎么进行Mock单元测试。
接下来我将和大家一块儿学习JMockit,相应的测试例子我将逐步添加,也是一步一步总结的,毕竟自己项目中用到的测试很难覆盖上图的全部。
2.实战:
本文使用了Maven构建工具,如果没有使用Maven 的话,可以去Maven中央库:http://mvnrepository.com/ 或者官网: http://jmockit.org/ 去搜索下载相应的jar包。
JMockit 在Maven库中的地址:
代码语言:javascript复制<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.27</version>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit-coverage</artifactId>
<version>1.23</version>
</dependency>
本文使用到的其他jar如下:
代码语言:javascript复制<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
闲言少叙,直接上代码:
BeanEntity类:
代码语言:javascript复制/**
* Created by zhangzh on 2016/9/6.
*/
public class BeanEntity {
private String beanId;
private String beanName;
private String beanType;
public String getBeanId() {
return beanId;
}
public void setBeanId(String beanId) {
this.beanId = beanId;
}
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getBeanType() {
return beanType;
}
public void setBeanType(String beanType) {
this.beanType = beanType;
}
}
Dao层代码:
代码语言:javascript复制import java.util.Arrays;
import java.util.List;
/**
* Created by zhangzh on 2016/9/6.
*/
public class Dao {
public void insert(BeanEntity bean){
System.out.println("Dao insert");
}
public void remove(String id){
System.out.println("Dao remove");
}
public void update(String name,String type){
System.out.println("Dao update");
}
public List<BeanEntity> finds(String name){
System.out.println("Dao finds");
BeanEntity bean = new BeanEntity();
bean.setBeanId("24336461423");
bean.setBeanName("张三");
bean.setBeanType("学生");
return Arrays.asList(bean);
}
}
Biz层代码:
代码语言:javascript复制import org.apache.commons.lang3.StringUtils;
import java.util.List;
/**
* Created by zhangzh on 2016/9/6.
*/
public class Biz {
private Dao dao;
public void insert(String id, String name, String type) {
if (StringUtils.isAnyBlank(id, name, type)) {
return;
}
BeanEntity bean = new BeanEntity();
bean.setBeanType(type);
bean.setBeanId(id);
bean.setBeanName(name);
dao.insert(bean);
}
public void remove(String id) {
if (StringUtils.isBlank(id)) {
return;
}
dao.remove(id);
}
public void update(String name, String type) {
if (StringUtils.isAnyBlank(name, type)) {
return;
}
dao.update(name, type);
}
public String finds(String name) {
if (StringUtils.isBlank(name)) {
return null;
}
List<BeanEntity> beans = dao.finds(name);
StringBuilder sb = new StringBuilder();
sb.append("#");
for (BeanEntity bean : beans) {
sb.append(bean.getBeanType()).append("#");
}
return sb.toString();
}
public Dao getDao() {
return dao;
}
public void setDao(Dao dao) {
this.dao = dao;
}
}
Mock测试类代码:
代码语言:javascript复制import mockit.Expectations;
import mockit.Injectable;
import mockit.Verifications;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zhangzh on 2016/9/6.
*/
public class BizTest {
private Biz biz = new Biz();
@Injectable
Dao dao;
@Before
public void setUp() throws Exception {
biz.setDao(dao);
}
@Test
public void insertTest() {
biz.insert("111","Name","student");
new Verifications() {
{
dao.insert((BeanEntity)any);
times = 1;
}
};
}
@Test
public void insertNullTest() {
biz.insert("111",null,"student");
new Verifications() {
{
dao.insert((BeanEntity)any);
times = 0;
}
};
}
@Test
public void updateTest() {
biz.update("111","student");
new Verifications() {
{
dao.update("111",anyString);
times = 1;
}
};
}
@Test
public void removeTest() {
biz.remove("111");
new Verifications() {
{
dao.remove("111");
times = 1;
}
};
}
@Test
public void findTest() {
new Expectations(dao) {
{
dao.finds("name");
BeanEntity bean1 = new BeanEntity();
bean1.setBeanType("student");
bean1.setBeanName("name");
bean1.setBeanId("11111111");
BeanEntity bean2 = new BeanEntity();
bean2.setBeanType("teacher");
bean2.setBeanName("name");
bean2.setBeanId("2222222222222");
List<BeanEntity> beans = new ArrayList<BeanEntity>();
beans.add(bean1);
beans.add(bean2);
result = beans;
}};
String result = biz.finds("name");
Assert.isTrue(StringUtils.equals("#student#teacher#",result));
new Verifications() {
{
dao.finds("name");
times = 1;
}
};
}
}
待补充代码中的注释
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149585.html原文链接:https://javaforall.cn