Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with a clean & simple API. Mockito doesn’t give you hangover because the tests are very readable and they produce clean verification errors.
Mockito是一个非常不错的模拟框架。 它使您可以使用干净简单的API编写漂亮的测试。 Mockito不会给您带来麻烦,因为这些测试可读性强,并且会产生清晰的验证错误。
官方说明-how简要版本
mockito官方文档-详细版本
特性和动机github说明
其特性如下:
gradle仓库添加相关配置如下:
repositories { jcenter() }
dependencies { testCompile "org.mockito:mockito-core:2.+" }
之于maven的相关配置,可以搜索添加pom的相关依赖;
import static org.mockito.Mockito.*;
// mock creation
List mockedList = mock(List.class);
// using mock object - it does not throw any "unexpected interaction" exception
mockedList.add("one");
mockedList.clear();
// selective, explicit, highly readable verification
verify(mockedList).add("one");
verify(mockedList).clear();
// you can mock concrete classes, not only interfaces
LinkedList mockedList = mock(LinkedList.class);
// stubbing appears before the actual execution
when(mockedList.get(0)).thenReturn("first");
// the following prints "first"
System.out.println(mockedList.get(0));
// the following prints "null" because get(999) was not stubbed
System.out.println(mockedList.get(999));
public class ArticleManagerTest {
@Mock private ArticleCalculator calculator;
@Mock private ArticleDatabase database;
@Mock private UserProvider userProvider;
private ArticleManager manager;
when(mock.someMethod(anyString())).thenAnswer(
new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Object mock = invocation.getMock();
return "called with arguments: " + Arrays.toString(args);
}
});
//Following prints "called with arguments: [foo]"
System.out.println(mock.someMethod("foo"));
更多说明参加详细资料文档mockito官方文档-详细版本
Mockito提供vertify关键字来实现校验方法是否被调用
,其具体作用可总结如下:
@Test
public void update() throws Exception {
boolean result = personService.update(1, "new name");
//验证mockDao的getPeron从未被调用
verify(mockDao,never()).getPerson(1);
assertTrue("must true", result);
//验证是否执行过一次getPerson(1)
verify(mockDao, times(1)).getPerson(eq(1));
//验证是否执行过一次update
verify(mockDao, times(1)).update(isA(Person.class));
}
但是需要注意的是verify的使用限制于mack对象,对于普通对象不支持相关检测,否则会触发相关错误。
Argument passed to verify() is of type RegistrationMgrActor and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type RegistrationMgrActor and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
at *******************
1 test completed, 1 failed
FAILURE: Build failed with an exception.
* What went wrong:
项目中,有些函数需要处理某个服务的返回结果,而在对函数单元测试的时候,又不能启动那些服务,这里就可以利用Mockito工具。Mockito中的Mock和Spy都可用于拦截那些尚未实现或不期望被真实调用的对象和方法,并为其设置自定义行为。二者的区别在于:
1、Mock声明的对象,对函数的调用均执行mock(即虚假函数),不执行真正部分。
2、Spy声明的对象,对函数的调用均执行真正部分。
这是官方网站上给出的一些mockito使用备忘:
以下是本人的一些感悟: