Mockito入门

Mockito是一个流行的Mocking框架。它使用起来简单,学习成本很低,而且具有非常简洁的API,测试代码的可读性很高。因此它十分受欢迎,用 户群越来越多,很多的开源的软件也选择了Mockito。要想了解更多有关Mockito的信息,请访问它的官方网 站:http://mockito.org/ 

在开始使用Mockito之前,先简单的了解一下Stub和Mock的区别。 
Stub对象用来提供测试时所需要的测试数据,可以对各种交互设置相应的回应。例如我们可以设置方法调用的返回值等等。Mockito中 when(…).thenReturn(…) 这样的语法便是设置方法调用的返回值。另外也可以设置方法在何时调用会抛异常等。Mock对象用来验证测试中所依赖对象间的交互是否能够达到预期。 Mockito中用 verify(…).methodXxx(…) 语法来验证 methodXxx方法是否按照预期进行了调用。有关stub和mock的详细论述见,Martin Fowler文章《Mocks Aren't Stub》http://martinfowler.com/articles/mocksArentStubs.html。在Mocking框架中所谓 的mock对象实际上是作为上述的stub和mock对象同时使用的。因为它既可以设置方法调用返回值,又可以验证方法的调用。 

Mockito的获取 
Jar包的获取  http://code.google.com/p/mockito/downloads/list 

Maven 
如果项目是通过Maven管理的,需要在项目的Pom.xml中增加如下的依赖:

Java代码   收藏代码
  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. <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>  


Mocktio包的引入 
在程序中可以import org.mockito.Mockito;然后调用它的static方法,或者import static org.mockito.Mockito.*;

一个简单的例子

Java代码   收藏代码
  1. import   static  org.junit.Assert.*;  
  2. import   static  org.mockito.Mockito.*;  
  3. import  java.util.Iterator;  
  4. import  org.junit.Test;  
  5.   
  6. public   class  SimpleTest {  
  7.       
  8.     @Test   
  9.     public   void  simpleTest(){  
  10.         //arrange   
  11.         Iterator i=mock(Iterator.class );  
  12.         when(i.next()).thenReturn("Hello" ).thenReturn( "World" );  
  13.         //act   
  14.         String result=i.next()+" " +i.next();  
  15.         //verify   
  16. verify(i, times(2 )).next();  
  17.         //assert   
  18.         assertEquals("Hello World" , result);  
  19.     }  
  20. }  
Java代码   收藏代码
  1. import static org.junit.Assert.*;  
  2. import static org.mockito.Mockito.*;  
  3. import java.util.Iterator;  
  4. import org.junit.Test;  
  5.   
  6.  
  7. public class SimpleTest {  
  8.       
  9.     @Test  
  10.     public void simpleTest(){  
  11.         //arrange  
  12.         Iterator i=mock(Iterator.class);  
  13.         when(i.next()).thenReturn("Hello").thenReturn("World");  
  14.         //act  
  15.         String result=i.next()+" "+i.next();  
  16.         //verify  
  17. verify(i, times(2)).next();  
  18.         //assert  
  19.         assertEquals("Hello World", result);  
  20.     }  
  21. }  



在上面的例子中包含了Mockito的基本功能: 
创建Mock对象 
创建Mock对象的语法为,mock(class or interface)。例子中创建了Iterator接口的mock对象。 

设置方法调用的预期返回 
通过when(mock.someMethod()).thenReturn(value) 来设定mock对象某个方法调用时的返回值。例子中我们对Iterator接口的next()方法调用进行了预期设定,当调用next()方法时会返 回”Hello”,由于连续设定了返回值,因此当第二次调用时将返回”World”。 

验证方法调用 
接下来对mock对象的next()方法进行了一系列实际的调用。mock对象一旦建立便会自动记录自己的交互行为,所以我们可以有选择的对它的 交互行为进行验证。在Mockito中验证mock对象交互行为的方法是verify(mock).someMethod(…)。于是用此方法验证了 next()方法调用,因为调用了两次,所以在verify中我们指定了times参数(times的具体应用在后面会继续介绍)。最后assert返回 值是否和预期一样。


模拟对象:

  1. // 模拟LinkedList 的一个对象  
  2. LinkedList mockedList = mock(LinkedList.class);   
  3.   
  4. // 此时调用get方法,会返回null,因为还没有对方法调用的返回值做模拟   
  5. System.out.println(mockedList.get(999));  

模拟方法调用的返回值:

  比如

  1. // 模拟获取第一个元素时,返回字符串first。  给特定的方法调用返回固定值在官方说法中称为stub。
  2. when(mockedList.get(0)).thenReturn("first");   
  3.   
  4. // 此时打印输出first   
  5. System.out.println(mockedList.get(0));  

模拟方法调用抛出异常:

  1. // 模拟获取第二个元素时,抛出RuntimeException  
  2. when(mockedList.get(1)).thenThrow(new RuntimeException());   
  3.   
  4. // 此时将会抛出RuntimeException  
  5. System.out.println(mockedList.get(1));  
 没有返回值类型的方法也可以模拟异常抛出:

doThrow(new RuntimeException()).when(mockedList).clear();

 

模拟调用方法时的参数匹配:

  1. // anyInt()匹配任何int参数,这意味着参数为任意值,其返回值均是element  
  2. when(mockedList.get(anyInt())).thenReturn("element");   
  3.   
  4. // 此时打印是element   
  5. System.out.println(mockedList.get(999)); 

模拟方法调用次数:

  1. // 调用add一次   
  2. mockedList.add("once");   
  3.   
  4. // 下面两个写法验证效果一样,均验证add方法是否被调用了一次  
  5. verify(mockedList).add("once");   
  6. verify(mockedList, times(1)).add("once");  
 还可以通过atLeast(int i)和atMost(int i)来替代time(int i)来验证被调用的次数最小值和最大值。


 一篇很好的入门文章:

http://blog.csdn.net/huoshuxiao/archive/2010/12/30/6107835.aspx

 一些稍微复杂且实用一点的例子:

http://gojko.net/2009/10/23/mockito-in-six-easy-examples/


其它参考http://liuzhijun.iteye.com/blog/1512780


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值