Mockito入门

 

Mockito入门

关键字: 单元测试 模拟

简介

 

InfoQ-使用Mockito 1.5监视普通对象 写道
Mockito是一个针对Java的mocking框架。它与EasyMock和jMock很相似,但是通过在执行后校验什么已经被调用,它消除了对期望行为(expectations)的需要。其它的mocking库需要你在执行前记录期望行为(expectations),而这导致了丑陋的初始化代码。

 

 更多信息请参考如下位置,

 原文链接:http://www.infoq.com/cn/news/2008/09/mockito-1.5

 官方网站:http://code.google.com/p/mockito/

 

入门

以下内容均参考至http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mockito.html


在任何应用Mockito的地方,加上如下片段:
Java代码 复制代码
  1. import   static  org.mockito.Mockito.*;  
import static org.mockito.Mockito.*;
 

模拟对象

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

// 此时调用get方法,是会返回null,因为还没有对方法调用的返回值做模拟 
System.out.println(mockedList.get(999));

 

 

 模拟方法调用的返回值

 

Java代码 复制代码
  1. // 模拟获取第一个元素时,返回字符串first   
  2. when(mockedList.get( 0 )).thenReturn( "first" );   
  3.   
  4. // 此时打印输出first   
  5. System.out.println(mockedList.get( 0 ));  
// 模拟获取第一个元素时,返回字符串first
when(mockedList.get(0)).thenReturn("first");

// 此时打印输出first
System.out.println(mockedList.get(0));
 

模拟方法调用抛出异常

Java代码 复制代码
  1. // 模拟获取第二个元素时,抛出RuntimeException   
  2. when(mockedList.get( 1 )).thenThrow( new  RuntimeException());   
  3.   
  4. // 此时将会抛出RuntimeException   
  5. System.out.println(mockedList.get( 1 ));  
// 模拟获取第二个元素时,抛出RuntimeException
when(mockedList.get(1)).thenThrow(new RuntimeException());

// 此时将会抛出RuntimeException
System.out.println(mockedList.get(1));
 没有返回值类型的方法也可以模拟异常抛出:

Java代码 复制代码
  1. doThrow( new  RuntimeException()).when(mockedList).clear();  
doThrow(new RuntimeException()).when(mockedList).clear();
 

模拟方法调用的参数匹配

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

// 此时打印是element
System.out.println(mockedList.get(999));

验证方法调用次数

Java代码 复制代码
  1. // 调用add一次   
  2. mockedList.add( "once" );   
  3.   
  4. // 下面两个写法验证效果一样,均验证add方法是否被调用了一次   
  5. verify(mockedList).add( "once" );   
  6. verify(mockedList, times( 1 )).add( "once" );  
// 调用add一次
mockedList.add("once");

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

 

收尾

上面仅列举了一些常用的,更多用法和技巧还请详细参考 http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mockito.html 。让Mockito使测试驱动开发更有趣吧!

【EasyMock 使用方法与原理剖析】
http://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/

org.mockito.exceptions.Reporter
http://grepcode.com/file/repo1.maven.org/maven2/org.mockito/mockito-all/1.8.0/org/mockito/exceptions/Reporter.java

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值