1. 概述:
小伙伴们,想必你看到此文,估计也是被公司强制要求的单元测试覆盖率苦恼了,我想说:“me,too”。
鄙人不才,之前写单元测试,都是环境和上下文依赖的,其实算不上是Junit测试,有时候甚至在自己的类中写一个main方法,进行简单的测试,这些对于菜鸟和新手都是必须经历的过程,如果自己开发,我仍然会这样测试,但是对于公司来说,公司的Sonar或其他工具很难评价项目的代码质量,比如测试覆盖率等,没有覆盖的代码,就意味着风险,特别是对于质量就是生命的企业来说,更是如此。
对于一个项目,选择什么样的单元测试Mock框架十分重要,因为如果你要测试的方法,框架不支持那就麻烦了,只能望“码”兴叹了!
接下来我将和大家一块儿学习JMockit,相应的测试例子我将逐步添加,也是一步一步总结的,毕竟自己项目中用到的测试很难覆盖全部。
2.实战:
本文使用了Maven构建工具,如果没有使用Maven 的话,可以去Maven中央库:http://mvnrepository.com/ 或者官网:http://jmockit.org/ 去搜索下载相应的jar包。
JMockit 在Maven库中的地址:
<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></pre>
本文使用到的其他jar如下:
<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></pre>
闲言少叙,直接上代码:
2.1 BeanEntity类:
/**
* Created by lance 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;
}
}
2.1 Dao层代码:
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);
}
}
2.3 Biz层代码:
import org.apache.commons.lang3.StringUtils;
import java.util.List;
/**
* Created by lance 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;
}
}
2.4 Mock测试类代码:
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 lance 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;
}
};
}
}
看,很简单吧!