这是一篇旧文,之前在团队内部分享过,刚好在Google官方Android App架构蓝图的Sample里也使用了Mockito,就在这里发出来,希望给大家带来帮助。
为什么需要Mock
测试驱动的开发(Test Driven Design, TDD)要求我们先写单元测试,再写实现代码。在写单元测试的过程中,我们往往会遇到要测试的类有很多依赖,这些依赖的类/对象/资源又有别的依赖,从而形成一个大的依赖树,要在单元测试的环境中完整地构建这样的依赖,是一件很困难的事情。如下:
为了测试类A,我们需要Mock B类和C类
如何Mock
对那些不容易构建的对象用一个虚拟对象来代替,使其在调试期间用来作为真实对象的替代品。
Mockito介绍
Mockito是一个模拟测试框架,可以让你用优雅,简洁的接口写出漂亮的单元测试。Mockito可以让单元测试易于可读,产生简洁的校验错误。
使用场景
- 提前创建测试,TDD(测试驱动开发)
- 团队可以并行工作
- 你可以创建一个验证或者演示程序
- 为无法访问的资源编写测试
- Mock可以交给用户
- 隔离系统
Mockito入门(具体摘录官网,列出比较常用的功能)
- 引用包
repositories { jcenter() }
dependencies { testCompile "org.mockito:mockito-core:1.+" }
- 交互检验
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));
- 按顺序校验
personDAL.add(any());
personDAL.getAll();
InOrder inOrder = inOrder(personDAL);
inOrder.verify(personDAL).add(any());
inOrder.verify(personDAL).getAll();
- 校验某个行为没有发生
//using mocks - only mockOne is interacted
mockOne.add("one");
//ordinary verification
verify(mockOne).add("one");
//verify that method was never called on a mock
verify(mockOne, never()).add("two");
- 使用@Mock注解
@Mock private static PersonDAL personDAL2;
MockitoAnnotations.initMocks(PersonDAL.class);
- Mock真实的对象
List list = new LinkedList();
List spy = spy(list);
//optionally, you can stub out some methods:
when(spy.size()).thenReturn(100);
//using the spy calls *real* methods
spy.add("one");
spy.add("two");
//prints "one" - the first element of a list
System.out.println(spy.get(0));
//size() method was stubbed - 100 is printed
System.out.println(spy.size());
//optionally, you can verify
verify(spy).add("one");
verify(spy).add("two");
- 重置模拟
List mock = mock(List.class);
when(mock.size()).thenReturn(10);
mock.add(1);
reset(mock);
注意
不能对final,Anonymous ,primitive类进行mock。
设计原则(不翻译,体会原文)
- Only one type of mock, one way of creating mocks
- No framework-supporting code.
- Slim API.
设计之美
我们可以看到mockito设计的简洁优美,以之前的例子为例:
// 设置mock对象的行为 - 当调用其get方法获取第0个元素时,返回"first"
Mockito.when(mockedList.get(0)).thenReturn("first");
在Mock对象的时候,创建一个proxy对象,保存被调用的方法名(get),以及调用时候传递的参数(0),然后在调用thenReturn方法时再把“first”保存起来,这样,就有了构建一个stub方法所需的所有信息,构建一个stub。当get方法被调用的时候,实际上调用的是之前保存的proxy对象的get方法,返回之前保存的数据。
具体可以看下面的源码
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
InternalMockHandler mockitoHandler = cast(handler);
new AcrossJVMSerializationFeature().enableSerializationAcrossJVM(settings);
return new ClassImposterizer(new InstantiatorProvider().getInstantiator(settings)).imposterise(
new MethodInterceptorFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces());
}
public <T> T imposterise(final MethodInterceptor interceptor, Class<T> mockedType, Class<?>... ancillaryTypes) {
Class<Factory> proxyClass = null;
Object proxyInstance = null;
try {
setConstructorsAccessible(mockedType, true);
proxyClass = createProxyClass(mockedType, ancillaryTypes);
proxyInstance = createProxy(proxyClass, interceptor);
return mockedType.cast(proxyInstance);
} catch (ClassCastException cce) {
throw new MockitoException(join(
"ClassCastException occurred while creating the mockito proxy :",
" class to mock : " + describeClass(mockedType),
" created class : " + describeClass(proxyClass),
" proxy instance class : " + describeClass(proxyInstance),
" instance creation by : " + instantiator.getClass().getSimpleName(),
"",
"You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration)"
), cce);
} finally {
setConstructorsAccessible(mockedType, false);
}
}
相关资料
Mockito官网
Mockito文档
mockito github
mockito作者设计理念
反模式的经典 - Mockito设计解析
Mocks Aren't Stubs
欢迎关注我的微博