Mockito常用方法及示例

Mockit是一个开源mock框架,官网:http://mockito.org/,源码:https://github.com/mockito/mockito

要使用Mockit,首先需要在我们工程中引入对应的jar包,对于maven工程而言,需要添加如下依赖项即可:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.     <groupId>org.mockito</groupId>  
  3.     <artifactId>mockito-core</artifactId>  
  4.     <version>2.0.5-beta</version>  
  5. </dependency>  
而在我们实际使用时,为了组织测试case的需要,我们可能还需要testng:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.     <groupId>org.testng</groupId>  
  3.     <artifactId>testng</artifactId>  
  4.     <version>6.8.8</version>  
  5.     <scope>test</scope>  
  6. </dependency>  

在进行下面的mock test示例之前,我们先建两个简单的被测类Demo、ParameterClass。

Demo.java:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.anlegor.test;  
  2.   
  3. public class Demo {  
  4.   
  5.     private String name ="laowang";  
  6.     private int age;  
  7.   
  8.     public Demo(String name, int age) {  
  9.         this.name = name;  
  10.         this.age = age;  
  11.     }  
  12.   
  13.     public String speak(String str) {  
  14.         return str;  
  15.     }  
  16.     public String talk(String str)  
  17.     {  
  18.         return str;  
  19.     }  
  20.     public String methodNoParameters()  
  21.     {  
  22.         return name;  
  23.     }  
  24.   
  25.     public String methodCustomParameters(ParameterClass parameter,String str)  
  26.     {  
  27.         return str;  
  28.     }  
  29.   
  30.     public String methodHaveChildObj(ParameterClass parameter,String str)  
  31.     {  
  32.         parameter.childTalk(str);  
  33.         return str;  
  34.   
  35.     }  
  36. }  

ParameterClass.java

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.anlegor.test;  
  2.   
  3. public class ParameterClass {  
  4.   
  5.     public ParameterClass() {  
  6.   
  7.     }  
  8.   
  9.     public String childTalk(String str)  
  10.     {  
  11.         return str;  
  12.     }  
  13.   
  14. }  

我们在进行mock的时候,常见会有如下一些场景:

1、 构造无参函数的返回

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 测试无参数函数mock  
  3.  */  
  4. @Test(priority=0)  
  5. public void testReturnDirect()  
  6. {  
  7.     String mocked = "mocked Return";  
  8.     Demo demo  = Mockito.mock(Demo.class);  
  9.     Mockito.when(demo.methodNoParameters()).thenReturn(mocked);  
  10.     Assert.assertEquals(demo.methodNoParameters(), mocked);  
  11. }  

2、构造有基本类型作为参数的返回

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 测试任意基本类型参数函数mock  
  3.  */  
  4. @Test(priority=1)  
  5. public void testMethodWithParameter()  
  6. {  
  7.     String word"mocked Return";  
  8.     Demo demo =  Mockito.mock(Demo.class);  
  9.     Mockito.when(demo.speak(Mockito.anyString())).thenReturn(word);  
  10.     Assert.assertEquals(demo.speak("你好"), word);  
  11. }  

3、构造有基本类型作为参数,但是只针对特定参数输入才mock返回值

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 测试特定参数mock  
  3.  */  
  4. @Test(priority=2)  
  5. public void testMethodWithSpecificParameter()  
  6. {  
  7.     String word"mocked Return";  
  8.     Demo demo =  Mockito.mock(Demo.class);  
  9.     Mockito.when(demo.speak(Mockito.matches(".*大爷$"))).thenReturn(word);  
  10.     Assert.assertEquals(demo.speak("隔壁李大爷"), word);  
  11. }  

4、构造自定义类作为函数参数的返回,这种情况稍微复杂一些,需要实现一个matcher类

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 测试自定义类型参数的mock  
  3.  */  
  4. @Test(priority=3)  
  5. public void testMethodWithCustomParameter()  
  6. {  
  7.     String word"mocked Return";  
  8.     Demo demo =  Mockito.mock(Demo.class);  
  9.     Mockito.when(demo.methodCustomParameters(Mockito.argThat(new IsParameterClass()),  
  10.             Mockito.anyString())).thenReturn(word);  
  11.     Assert.assertEquals(demo.methodCustomParameters(new ParameterClass(), "你猜"), word);  
  12. }  
  13. //自定义一个与之匹配的matcher类  
  14. class IsParameterClass extends ArgumentMatcher<ParameterClass> {  
  15.     public boolean matches(Object para) {  
  16.         return para.getClass() == ParameterClass.class;  
  17.     }  
  18.  }  

5、构造null返回

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 测试mock的函数返回null  
  3.  */  
  4. @Test(priority=4)  
  5. public void testMethodWithReturnNull()  
  6. {  
  7.     String word"mocked Return";  
  8.     Demo demo =  Mockito.mock(Demo.class);  
  9.     Mockito.when(demo.speak(Mockito.anyString())).thenReturn(null);  
  10.     Assert.assertNotEquals(demo.speak("你猜"), word);  
  11. }  

6、构造mock的函数抛出异常,当然我们可以在testng中设置expectedExceptions以显示声明会抛出指定类型的异常,这样该条case执行的时候就会成功

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 测试mock的函数抛出异常  
  3.  */  
  4. @Test(expectedExceptions=org.mockito.exceptions.base.MockitoException.class,priority=5)  
  5. public void testMethodReturnException()  
  6. {  
  7.     String word"mocked Return";  
  8.     Demo demo =  Mockito.mock(Demo.class);  
  9.     Mockito.when(demo.speak(Mockito.anyString())).thenThrow(new Exception());  
  10.     demo.speak("你猜");  
  11. }  

7、某些反复调用,我们希望对于每次调用都返回不同的mock值

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 测试mock的不同次调用返回不同的值  
  3.  */  
  4. @Test(priority=6)  
  5. public void testMethodMultiDiffReturn()  
  6. {  
  7.     String word"mocked Return 0";  
  8.     String word1"mocked Return 1";  
  9.     Demo demo =  Mockito.mock(Demo.class);  
  10.     Mockito.when(demo.speak(Mockito.anyString())).thenReturn(word).thenReturn(word1);  
  11.     Assert.assertEquals(demo.speak("你猜"), word);  
  12.     Assert.assertEquals(demo.speak("你猜"), word1);  
  13. }  

8、验证函数执行是否经过了mock

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 验证方法是否被mock且是否为所执行的参数调用  
  3.  */  
  4. @Test(expectedExceptionsorg.mockito.exceptions.misusing.NotAMockException.class,priority=7)  
  5. public void testMockedMethodRun()  
  6. {  
  7.     String word"mocked Return";  
  8.     Demo demo =  Mockito.mock(Demo.class);  
  9.     Mockito.when(demo.speak(Mockito.anyString())).thenReturn(word);  
  10.     Assert.assertEquals(demo.speak("你猜"), word);  
  11.     Mockito.verify(demo.speak("你猜"));  
  12.     //下面这个参数的方法调用并没有被执行过,所以会抛出NotAMockException的异常  
  13.     Mockito.verify(demo.speak("nicai"));  
  14. }  

如果对于上面的反复使用Mockito.when***的写法很厌烦的话,就直接静态导入org.mockito.Mockito.*即可。

当然,mockito的作用也不仅仅如上面,更详细的使用可以 参考它很详细的帮助文档:

http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值