基本使用步骤参考
-
首先你需要有待测试的类或接口类型。
-
用Mockito.mock或Mockito.spy静态函数创建这些类型的mock对象。
-
最后使用Mockito.when,Mockito.doReturn等api给mock对象函数打桩,就像现实生活中前台客服学习客套话差不多。
到此就可以使用mock对象模拟测试类型的操作了,并且Mockito框架会监控统计mock对象调用记录,可以使用Mockito.verify,Mockito.times等函数验证调用行为或调用次数等操作。
参考源码
声明以下源码范例来自 【Mockito 中文文档】
验证某些行为
// 静态导入会使代码更简洁
import static org.mockito.Mockito.*;
// mock creation 创建mock对象
List mockedList = mock(List.class);
//using mock object 使用mock对象
mockedList.add("one");
mockedList.clear();
//verification 验证
verify(mockedList).add("one");
verify(mockedList).clear();
如何做一些测试桩 (Stub)
//You can mock concrete classes, not only interfaces
// 你可以mock具体的类型,不仅只是接口
LinkedList mockedList = mock(LinkedList.class);
//stubbing
// 测试桩
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
//following prints "first"
// 输出“first”
System.out.println(mockedList.get(0));
//following throws runtime exception
// 抛出异常
System.out.println(mockedList.get(1));
//following prints "null" because get(999) was not stubbed
// 因为get(999) 没有打桩,因此输出null
System.out.println(mockedList.get(999));
//Although it is possible to verify a stubbed invocation, usually it's just redundant
//If your code cares what get(0) returns then something else breaks (often before even verify() gets executed).
//If your code doesn't care what get(0) returns then it should not be stubbed. Not convinced? See here.
// 验证get(0)被调用的次数
verify(mockedList).get(0);
参数匹配器 (matchers)
//stubbing using built-in anyInt() argument matcher
// 使用内置的anyInt()参数匹配器
when(mockedList.get(anyInt())).thenReturn("element");
//stubbing using custom matcher (let's say isValid() returns your own matcher implementation):
// 使用自定义的参数匹配器( 在isValid()函数中返回你自己的匹配器实现 )
when(mockedList.contains(argThat(isValid()))).thenReturn("element");
//following prints "element"
// 输出element
System.out.println(mockedList.get(999));
//you can also verify using an argument matcher
// 你也可以验证参数匹配器
verify(mockedList).get(anyInt());
verify(mock).someMethod(anyInt(), anyString(), eq("third argument"));
//above is correct - eq() is also an argument matcher
// 上述代码是正确的,因为eq()也是一个参数匹配器
verify(mock).someMethod(anyInt(), anyString(), "third argument");
//above is incorrect - exception will be thrown because third argument
// 上述代码是错误的,因为所有参数必须由匹配器提供,而参数"third argument"并非由参数匹配器提供,因此的缘故会抛出异常
为返回值为void的函数通过Stub抛出异常
doThrow(new RuntimeException()).when(mockedList).clear();
//following throws RuntimeException:
// 调用这句代码会抛出异常
mockedList.clear();
验证函数的确切、最少、从未调用次数
//using mock
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
//following two verifications work exactly the same - times(1) is used by default
// 下面的两个验证函数效果一样,因为verify默认验证的就是times(1)
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
//exact number of invocations verification
// 验证具体的执行次数
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
//verification using never(). never() is an alias to times(0)
// 使用never()进行验证,never相当于times(0)
verify(mockedList, never()).add("never happened");
//verification using atLeast()/atMost()
// 使用atLeast()/atMost()
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("five times");
verify(mockedList, atMost(5)).add("three times");
为回调做测试桩
when(mock.someMethod(anyString())).thenAnswer(new Answer() {
Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Object mock = invocation.getMock();
return "called with arguments: " + args;
}
});
//Following prints "called with arguments: foo"
// 输出 : "called with arguments: foo"
System.out.println(mock.someMethod("foo"));
API参考
以下内容来自https://blog.csdn.net/xiao__jia__jia/article/details/115252780
org.mockito.Mockito是mockito提供的核心api,提供了大量的静态方法,用于帮助我们来mock对象,验证行为等等,然后需要注意的是,很多方法都被封装在了MockitoCore类里面,下面对一些常用的方法做一些介绍。
mock:构建一个我们需要的对象;可以mock具体的对象,也可以mock接口。
spy:构建监控对象
verify:验证某种行为
when:当执行什么操作的时候,一般配合thenXXX 一起使用。表示执行了一个操作之后产生什么效果。
doReturn:返回什么结果
doThrow:抛出一个指定异常
doAnswer:做一个什么相应,需要我们自定义Answer;
times:某个操作执行了多少次
atLeastOnce:某个操作至少执行一次
atLeast:某个操作至少执行指定次数
atMost:某个操作至多执行指定次数
atMostOnce:某个操作至多执行一次
doNothing:不做任何处理
doReturn:返回一个结果
doThrow:抛出一个指定异常
doAnswer:指定一个操作,传入Answer
doCallRealMethod:返回真实业务执行的结果,只能用于监控对象
ArgumentMatchers
用于进行参数匹配,减少很多不必要的代码
anyInt:任何int类型的参数,类似的还有anyLong/anyByte等等。
eq:等于某个值的时候,如果是对象类型的,则看toString方法
isA:匹配某种类型
matches:使用正则表达式进行匹配
OngoingStubbing
OngoingStubbing用于返回操作的结果。
thenReturn:指定一个返回的值
thenThrow:抛出一个指定异常
then:指定一个操作,需要传入自定义Answer;
thenCallRealMethod:返回真实业务执行的结果,只能用于监控对象。