Jmockit可以做什么

使用JMockit API来mock被依赖的代码,从而进行隔离测试。

  • 类级别整体mock和部分方法重写
  • 实例级别整体mock和部分mock
  • mock静态方法、私有变量、局部方法
  • 灵活的参数匹配

maven依赖

Jmockit可以和junit和TestNG配合使用。需要注意的是:

  • 如果使用Junit4.5以上,jmockit依赖需要在junit4之前;或者在测试类上添加注解 @RunWith(JMockit.class)。
  • 如果是TestNG 6.2+ 或者 JUnit 5+, 没有位置限制
		<!-- 如果使用Junit4.5以上 jmockit依赖需要在junit4之前 -->
<!-- 或者在测试类上添加注解 @RunWith(JMockit.class) -->
<!-- 如果是TestNG 6.2+ 或者 JUnit 5+, 没有位置限制 -->
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

版本记录 版本更新较快。

使用

涉及到三个类:

  • 测试类:执行测试代码的类。
  • CUT(Code Under Test):被测试的类,测试此类是否能正确地工作。
  • 依赖类:CUT会调用依赖类的方法。

CUT

@Data
public class Person { private String name;
private Integer age;
private Person friend; public Person(){} public Person(String name, Integer age, Person friend){
this.age = age;
this.name = name;
this.friend = friend;
} @Override
public boolean equals(Object obj) {
return this.name.equals(((Person)obj).getName());
}
} @Service
public class PersonService { public String showName(String name){
System.out.println("person show name : " + name);
return name;
} public int showAge(int age) {
System.out.println("person show age : " + age);
return age;
} public Person getDefaultPerson(){
return new Person("miao", 3, null);
}
} @Service
public class CoderService {
@Value("${coder.service.desc}")
private String desc; public String showWork(String work){
return work;
} public int showSalary(int salary){
return salary;
} public String getDesc() {
return desc;
} public String getPersonName(Person person){
return person.getName();
}
}

基本流程

record(录制)---- replay(回放) ---- verify(验证)

record : 设置将要被调用的方法和返回值。

  • Expections中的方法至少被调用一次,否则会出现missing invocation错误。调用次数和调用顺序不限。
  • StrictExpectations中方法调用的次数和顺序都必须严格执行。如果出现了在StrictExpectations中没有声明的方法,会出现unexpected invocation错误。

replay:调用(未被)录制的方法,被录制的方法调用会被JMockit拦截并重定向到record阶段设定的行为。

verify:基于行为的验证,测试CUT是否正确调用了依赖类,包括:调用了哪些方法;通过怎样的参数;调用了多少次;调用的相对顺序(VerificationsInOrder)等。可以使用times,minTimes,maxTimes来验证。

    @Test
public void mockProcessTest(final @Mocked PersonService target){
//录制预期行为
new Expectations(){
{
target.showName(anyString);
result = "test1";
target.showAge(anyInt);
result = -1;
}
}; //测试代码
Assert.assertTrue("test1".equals(target.showName("test2")));
Assert.assertTrue(-1 == target.showAge(12));
Assert.assertTrue(-1 == target.showAge(12)); //验证
new Verifications(){
{
target.showName("test1");
times = 0; //执行了0次。参数一致的才会计数
target.showAge(12);
times = 2; //执行了2次
}
};
} /**
* Expections中的方法至少被调用一次,否则会出现missing invocation错误.
* 调用次数和调用顺序不限.
*/
@Test
public void mockExpectationsProcessTest(final @Mocked PersonService service){
new Expectations(){{
service.showAge(anyInt);
result = -1;
}};
//只调用showName会报错 Missing 1 invocation
service.showName("hahah");
service.showAge(12);
}
/**
* StrictExpectations中方法调用的次数和顺序都必须严格执行。如果出现了在StrictExpectations中没有声明的方法,会出现unexpected invocation错误。
* 没有必要做Verifications验证。
*/
@Test
public void mockStrictExpectationsProcessTest(final @Mocked PersonService service){
new StrictExpectations(){{
service.showAge(anyInt);
result = -1;
service.showName(anyString);
result = "ok";
}}; //1.下面只执行了一个录制方法,报错:unexpected invocation, Missing invocation
// Assert.assertTrue(-1 == service.showAge(12)); //2.下面与录制顺序不一致,会报错:unexpected invocation, Missing invocation
// Assert.assertTrue("ok".equals(service.showName("test")));
// Assert.assertTrue(-1 == service.showAge(12)); //3.调用没有录制的方法,报错 Unexpected invocation
// service.getDefaultPerson(); //必须全部执行录制的方法,且顺序一致
Assert.assertTrue(-1 == service.showAge(12));
Assert.assertTrue("ok".equals(service.showName("test")));
}

部分注解说明

RunWith(JMockit.class): 指定单元测试的执行类为JMockit.class。

Tested: 指定被测试类,同时mock实例并注入测试类;依赖的类使用Injectable注入。

Injectable: 将对象进行mock并注入测试类。

Mocked:mock一种类型,并注入测试类。

Mocked与Injectable区别:

  • Mocked 注入的依赖,类的所有实例都被mock,record的方法,在replay时,按照record的结果返回;没有record的方法返回默认值。
  • Injectable 注入的依赖,只mock指定的实例,record的方法,在replay时,按照record的结果返回;没有record的方法返回默认值。没有mock的实例,调用其原始方法。
@RunWith(JMockit.class)
public class MockTest { //@Mocked 修饰,所有实例都会被mock
@Mocked
private PersonService personService; // @Injectable 修饰,只mock指定的实例。
@Injectable
private CoderService coderService; @Test
public void testInstance(){
new Expectations(){
{
personService.showAge(anyInt);
result = -1; personService.getDefaultPerson();
result = new Person("me", 4, null); Deencapsulation.invoke(coderService, "showWork", anyString);
result = "java"; }
}; //record的方法,按照给定的结果返回
Assert.assertTrue(-1 == personService.showAge(11));
Assert.assertTrue("java".equals(coderService.showWork("nothing")));
Assert.assertTrue(4 == personService.getDefaultPerson().getAge());
//没有录制的方法,返回默认值
Assert.assertTrue(personService.showName("testName") == null);
Assert.assertTrue(coderService.showSalary(100) == 0); //Mock 所有PersonServiceImpl实例
PersonService pservice = new PersonService();
Assert.assertTrue(-1 == pservice.showAge(11));
Assert.assertTrue(pservice.showName("testName") == null); //新生成的CoderService实例没有被mock
CoderService cservice = new CoderService();
Assert.assertTrue("something".equals(cservice.showWork("something")));
Assert.assertTrue(cservice.showSalary(100) == 100);
} /**
* 可以将参数注入,与类中注入结果一致。
* 但是不要同时在参数中注入,且在测试类中注入,会影响执行结果。
*/
@Test
public void testInjectObj(final @Injectable CoderService coderService){
new Expectations(){
{
coderService.showWork(anyString);
result = "ok";
}
};
Assert.assertTrue("ok".equals(coderService.showWork("hello")));
Assert.assertTrue(coderService.showSalary(100) == 0);
}

使用示例

  • 部分mock(实例级别)

    在Expectations中传入被mock实例。 则replay的方法在Expectations中被录制时,按照record结果返回;没有被录制,则调用原有代码。

    与之对应的是Injectable 注入的实例,record的方法,在replay时按照record结果返回;没有record的方法,返回默认值。
    /**
* 部分mock,在Expectations中传入被mock实例。
* replay的方法在Expectations中被录制时,按照record结果返回;
* 没有被录制,则调用原有代码
*/
@Test
public void partiallyMock(){
new Expectations(personService){
{
personService.showAge(anyInt);
result = -1;
}
};
//被录制的方法,按照record结果返回
Assert.assertTrue(-1 == personService.showAge(11));
//未录制的方法,调用原有代码
Assert.assertTrue("testName".equals(personService.showName("testName")));
}
  • mockUp(类级别)

    mockUp的类,被mock的方法,replay的时候都执行mock的方法;没有被mock的方法,调用原有代码。

    与之对应的事Mocked注入的类,所有record的方法按照record结果返回;没有record的方法,返回默认值。
    /**
* mockUp类,被mock的方法,replay的时候都执行mock的方法;
* 没有被mock的方法,调用原有代码
*/
@Test
public void mockUpTest(){ new MockUp<PersonService>(){
@Mock
public String showName(String name){
return "mocked";
}
}; Assert.assertTrue("mocked".equals(new PersonService().showName("test")));
Assert.assertTrue(1 == new PersonService().showAge(1));
}
  • mock 静态方法
    @Test
public void testStaticMethod(){
new Expectations(CollectionUtils.class){{
CollectionUtils.isEmpty((Collection<?>) any);
result = true;
}};
List<Integer> list = Lists.newArrayList(1,2,3);
Assert.assertTrue(list.size() == 3);
Assert.assertTrue(CollectionUtils.isEmpty(list));
}
  • mock 私有变量
  • 局部方法
  • mock 参数匹配问题

    参数为基本类型时,若mock方法参数设置为anyXXX,则任意此类型参数都可mock成功;若mock方法参数为具体值,则实际参数 equals mock参数时,才能mock成功。

    参数为非基本类型时,mock参数不可以为any,执行报错;若mock参数为具体值,只有传递的参数 equals mock参数时,才能mock成功。
public class CoderServiceTest {

    @Tested
private CoderService coderService;
@Injectable
private PersonService personService; @Test
public void testMockCase(){
new Expectations(coderService){{
//mock私有变量
Deencapsulation.setField(coderService, "desc", "coderDesc");
//mock 方法
Deencapsulation.invoke(coderService, "showWork", anyString);
result = "noWork";
}}; //mock 私有变量成功
Assert.assertTrue(coderService.getDesc().equals("coderDesc"));
//mock 私有方法
Assert.assertTrue(coderService.showWork("coder").equals("noWork"));
} @Test
public void testParamCase(){
new Expectations(coderService){{
//基本类型,mock参数为anyXXX
Deencapsulation.invoke(coderService, "showWork", anyString);
result = "mocked"; //基本类型,mock参数为实际值
Deencapsulation.invoke(coderService, "showSalary", 12);
result = -1; //非基本类型,mock参数不可以为anyXXX,会报错 java.lang.IllegalArgumentException: Invalid null value passed as argument 0
// Deencapsulation.invoke(coderService, "getPersonName", (Person)any);
// result = "mocked"; //基本类型,mock参数为实际值
Deencapsulation.invoke(coderService, "getPersonName", new Person("me", 3, null));
result = "mocked";
}}; //基本类型,mock参数为anyXXX, 实际参数为任意值mock成功
Assert.assertTrue(coderService.showWork("java").equals("mocked")); //基本类型,mock参数为具体值, 实际参数 equals mock参数时,mock成功
Assert.assertTrue(coderService.showSalary(12) == -1);
Assert.assertTrue(coderService.showSalary(100) == 100); //基本类型,mock参数为实际值,实际参数 equals mock参数时,mock成功
Assert.assertTrue("mocked".equals(coderService.getPersonName(new Person("me", 4, null))));
Assert.assertFalse("mocked".equals(coderService.getPersonName(new Person("you", 3, null))));
} }

注意事项

  • Tested指定的被测试类,必须是实现类,而非接口。否则不能正确实例化,报错NPE。

参考

JMockit git book 很挫!!!

JMockit Tutorial 很详细,部分示例已过时,推荐指数四颗星

官网 英文版

JMockit mock 实例使用的是asm技术

JMockit使用总结的更多相关文章

  1. JMockit

    [TOC] 简介 JMockit是基于JavaSE5中的java.lang.instrument包开发,内部使用ASM库来动态修改java的字节码,使得java这种静态语言可以想动态脚本语言一样动态设 ...

  2. Jmockit使用

    引用单元测试中mock的使用及mock神器jmockit实践中的java单元测试中各种Mock框架对比,就能明白JMockit有多么强大: JMockit是基于JavaSE5中的java.lang.i ...

  3. 使用JUnit4与JMockit进行打桩测试

    1. 何为Mock 项目中各个模块,各个类之间会有互相依赖的关系,在单元测试中,我们只关心被测试的单元,对于其依赖的单元并不关心(会有另外针对该单元的测试). 比如,逻辑层A类依赖了数据访问层B类的取 ...

  4. jmockit学习

    下图为jmockit 类图.在我们编写代码时几乎都会用到Expectations(期望)和Verifications(校验),二者均继承自Invacations. 常会用到的注解有:@Mocked @ ...

  5. jmockit学习总结

    mock类型和实例 从依赖的测试代码调用的方法和构造函数是mock(模拟)的目标. Mocking提供了我们需要的机制,以便将被测试的代码与(一些)依赖关系隔离开来.我们通过声明适当的模拟字段和/或模 ...

  6. Jmockit之mock特性详解

    本文是Jmockit学习过程中,根据官网所列的工具特性进行解读. 1.调用次数约束(Invocation count constraints) 可以通过调用计数约束来指定预期和/或允许匹配给定期望的调 ...

  7. 单元测试系列:Mock工具Jmockit使用介绍

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6760272.html Mock工具Jm ...

  8. JMockit常用操作

    JMockit常用操作 2017-11-30 转自:http://blog.csdn.net/foreverling/article/details/51234149 目录 1 基本概念  1.1 常 ...

  9. 单元测试系列之十一:Jmockit之mock特性详解

    本文是Jmockit学习过程中,根据官网所列的工具特性进行解读. 1.调用次数约束(Invocation count constraints) 可以通过调用计数约束来指定预期和/或允许匹配给定期望的调 ...

随机推荐

  1. (MVC)javaBaen+jsp+servlet对数据的操作

    运用MVC对数据进行一些简单的处理,基本实现数据的增删改查,达到前端和后台的数据之间的交互. 1.开始界面 <%@page import="com.zdsofe.work.Studen ...

  2. 基于三台主机部署phpwind

    PHPWind(简称:PW)的使命是让网站更具价值,让更多人从网络中享受便利,以提升生活品质. phpwind是一个基于PHP和MySQL的开源社区程序,是国内最受欢迎的通用型论坛程序之一.phpwi ...

  3. Java设计模式汇总

    Java设计模式汇总 设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式. ...

  4. ABP+AdminLTE+Bootstrap Table权限管理系统第八节--ABP错误机制及AbpSession相关

    上一节我们讲到登录逻辑,我做的登录逻辑很简单的,我们来看一下abp module-zero里面的登录代码. #region Login / Logout public ActionResult Log ...

  5. SecureCRT 常用命令大全

    常用命令:一.ls 只列出文件名 (相当于dir,dir也可以使用) -A:列出所有文件,包含隐藏文件. -l:列表形式,包含文件的绝大部分属性. -R:递归显示. --help:此命令的帮助. 二. ...

  6. asp.net 程序,当发生找不到文件的错误时,如何正确定位是哪个文件?

    需要在Global.asax.cs中添加Application_Error代码如下,在Log中查看是哪个文件缺失: protected void Application_Error(object se ...

  7. java 使用https协议,cas认证PKIX path building failed错误解决方法

    如果遇到的是 上图的异常,请继续往下看. linux 下 添加 证书 (1) 获取网站安全证书 xx.cer ( 详情见随笔 获取网站安全证书 ) (2) 将上面导出的证书导入java中的cacert ...

  8. C语言库函数探究

    1.strlen()求字符串长度 //模拟实现strlen函数 #include<stdio.h> #include<stdlib.h> #include<string. ...

  9. react入门之使用react-bootstrap当轮子造车(二)

    react入门之使用react-bootstrap当轮子造车(二) 上一篇我们谈了谈如何配置react的webpack环境 react入门之搭配环境(一) 可能很多人已经打开过官方文档学习了react ...

  10. PL/SQL 编程(二)游标、存储过程、函数

    游标--数据的缓存区 游标:类似集合,可以让用户像操作数组一样操作查询出来的数据集,实质上,它提供了一种从集合性质的结果中提取单条记录的手段. 可以将游标形象的看成一个变动的光标,他实质上是一个指针, ...