5分钟了解Mockito

 一、什么是mock测试,什么是mock对象?

 

先来看看下面这个示例:

从上图可以看出如果我们要对A进行测试,那么就要先把整个依赖树构建出来,也就是BCDE的实例。

 

一种替代方案就是使用mocks

从图中可以清晰的看出

mock对象就在调试期间用来作为真实对象的替代品

mock测试就是在测试过程中,对那些不容易构建的对象用一个虚拟对象来代替测试的方法就叫mock测试。

知道什么是mock测试后,那么我们就来认识一下mock框架---Mockito

 

二、什么是Mockito

除了有一个好记的名字外,Mockito尝试用不一样的方法做mocking测试,是简单轻量级能够替代EasyMock的框架。使用简单,测试代码可读性高,丰富的文档包含在javadoc中,直接在IDE中可查看文档,实例,说明。更多信息:http://code.google.com/p/mockito/

 

三、Stub和Mock

相同点:Stub和Mock对象都是用来模拟外部依赖,使我们能控制。

不同点:而stub完全是模拟一个外部依赖,用来提供测试时所需要的测试数据。而mock对象用来判断测试是否能通过,也就是用来验证测试中依赖对象间的交互能否达到预期。在mocking框架中mock对象可以同时作为stub和mock对象使用,两者并没有严格区别。 更多信息:http://martinfowler.com/articles/mocksArentStubs.html

 

四、mockito入门实例

Maven依赖:(没用maven管理的可以下载相关jar包导入classpath)

 

Xml代码   收藏代码
  1. <dependencies>    
  2. <dependency>    
  3. <groupId>org.mockito</groupId>    
  4. <artifactId>mockito-all</artifactId>    
  5. <version>1.8.5</version>    
  6. <scope>test</scope>    
  7. </dependency>    
  8. </dependencies>  

 

Java代码   收藏代码
  1. import static org.mockito.Mockito.*;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.junit.Assert;  
  6. import org.junit.Test;  
  7.   
  8. /** 
  9.  *  
  10.  * @author lzjun 
  11.  * @version 0.1 
  12.  * @date 2012-5-5 
  13.  * {@link http://weibo.com/u/1697702241}  
  14.  * 
  15.  */  
  16. public class SimpleTest {  
  17.           
  18.     @Test  
  19.     public void simpleTest(){  
  20.           
  21.         //创建mock对象,参数可以是类,也可以是接口  
  22.         List<String> list = mock(List.class);  
  23.           
  24.         //设置方法的预期返回值  
  25.         when(list.get(0)).thenReturn("helloworld");  
  26.       
  27.         String result = list.get(0);  
  28.           
  29.         //验证方法调用(是否调用了get(0))  
  30.         verify(list).get(0);  
  31.           
  32.         //junit测试  
  33.         Assert.assertEquals("helloworld", result);  
  34.     }  
  35. }  

好了,五分钟差不多了,还想继续了解那就可以往下面看

 

创建mock对象不能对final,Anonymous ,primitive类进行mock。

 

可对方法设定返回异常

 

Java代码   收藏代码
  1. when(list.get(1)).thenThrow(new RuntimeException("test excpetion"));  

 

stubbing另一种语法(设置预期值的方法),可读性不如前者

Java代码   收藏代码
  1. doReturn("secondhello").when(list).get(1);  

没有返回值的void方法与其设定(支持迭代风格,第一次调用donothing,第二次dothrow抛出runtime异常)

Java代码   收藏代码
  1. doNothing().doThrow(new RuntimeException("void exception")).when(list).clear();  
  2. list.clear();  
  3. list.clear();  
  4. verify(list,times(2)).clear();  

 

五、参数匹配器(Argument Matcher)

Matchers类内加你有很多参数匹配器  anyInt、anyString、anyMap.....Mockito类继承于Matchers,Stubbing时使用内建参数匹配器,下例:

 

Java代码   收藏代码
  1. @Test  
  2. public void argumentMatcherTest(){  
  3.       
  4.     List<String> list = mock(List.class);  
  5.       
  6.     when(list.get(anyInt())).thenReturn("hello","world");  
  7.       
  8.     String result = list.get(0)+list.get(1);  
  9.       
  10.     verify(list,times(2)).get(anyInt());  
  11.       
  12.     Assert.assertEquals("helloworld", result);  
  13.       
  14. }  

 

 需要注意的是:如果使用参数匹配器,那么所有的参数都要使用参数匹配器,不管是stubbing还是verify的时候都一样。

 

Java代码   收藏代码
  1. @Test  
  2. public void argumentMatcherTest2(){  
  3.       
  4.     Map<Integer,String> map = mock(Map.class);  
  5.     when(map.put(anyInt(),anyString())).thenReturn("hello");//anyString()替换成"hello"就会报错  
  6.     map.put(1"world");  
  7.     verify(map).put(eq(1), eq("world")); //eq("world")替换成"world"也会报错  
  8.       
  9. }  

 

 六、方法调用的验证(具体的调用次数、至少一次,一次也没有)

 

Java代码   收藏代码
  1. @Test  
  2. public void verifyInvocate(){  
  3.       
  4.     List<String> mockedList = mock(List.class);  
  5.     //using mock   
  6.      mockedList.add("once");  
  7.      mockedList.add("twice");  
  8.      mockedList.add("twice");  
  9.        
  10.      mockedList.add("three times");  
  11.      mockedList.add("three times");  
  12.      mockedList.add("three times");  
  13.        
  14.      /** 
  15.       * 基本的验证方法 
  16.       * verify方法验证mock对象是否有没有调用mockedList.add("once")方法 
  17.       * 不关心其是否有返回值,如果没有调用测试失败。 
  18.       */  
  19.      verify(mockedList).add("once");   
  20.      verify(mockedList, times(1)).add("once");//默认调用一次,times(1)可以省略  
  21.        
  22.        
  23.      verify(mockedList, times(2)).add("twice");  
  24.      verify(mockedList, times(3)).add("three times");  
  25.        
  26.      //never()等同于time(0),一次也没有调用  
  27.      verify(mockedList, times(0)).add("never happened");  
  28.        
  29.      //atLeastOnece/atLeast()/atMost()  
  30.      verify(mockedList, atLeastOnce()).add("three times");  
  31.      verify(mockedList, atLeast(2)).add("twice");  
  32.      verify(mockedList, atMost(5)).add("three times");  
  33.   
  34. }  
 

 

一次写不完,慢慢分析。。。

参考:

http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mockito.html

http://www.sizovpoint.com/2009/03/java-mock-frameworks-comparison.html

http://wenku.baidu.com/view/8def451a227916888486d73f.html

http://qiuguo0205.iteye.com/blog/1443344

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值