這是一篇舊文,之前在團(tuán)隊(duì)內(nèi)部分享過(guò),剛好在Google官方Android App架構(gòu)藍(lán)圖的Sample里也使用了Mockito,就在這里發(fā)出來(lái),希望給大家?guī)?lái)幫助。
為什么需要Mock
測(cè)試驅(qū)動(dòng)的開(kāi)發(fā)(Test Driven Design, TDD)要求我們先寫(xiě)單元測(cè)試,再寫(xiě)實(shí)現(xiàn)代碼。在寫(xiě)單元測(cè)試的過(guò)程中,我們往往會(huì)遇到要測(cè)試的類(lèi)有很多依賴,這些依賴的類(lèi)/對(duì)象/資源又有別的依賴,從而形成一個(gè)大的依賴樹(shù),要在單元測(cè)試的環(huán)境中完整地構(gòu)建這樣的依賴,是一件很困難的事情。如下:

為了測(cè)試類(lèi)A,我們需要Mock B類(lèi)和C類(lèi)

如何Mock
對(duì)那些不容易構(gòu)建的對(duì)象用一個(gè)虛擬對(duì)象來(lái)代替,使其在調(diào)試期間用來(lái)作為真實(shí)對(duì)象的替代品。
Mockito介紹
Mockito是一個(gè)模擬測(cè)試框架,可以讓你用優(yōu)雅,簡(jiǎn)潔的接口寫(xiě)出漂亮的單元測(cè)試。Mockito可以讓單元測(cè)試易于可讀,產(chǎn)生簡(jiǎn)潔的校驗(yàn)錯(cuò)誤。
使用場(chǎng)景
- 提前創(chuàng)建測(cè)試,TDD(測(cè)試驅(qū)動(dòng)開(kāi)發(fā))
- 團(tuán)隊(duì)可以并行工作
- 你可以創(chuàng)建一個(gè)驗(yàn)證或者演示程序
- 為無(wú)法訪問(wèn)的資源編寫(xiě)測(cè)試
- Mock可以交給用戶
- 隔離系統(tǒng)
Mockito入門(mén)(具體摘錄官網(wǎng),列出比較常用的功能)
- 引用包
repositories { jcenter() }
dependencies { testCompile "org.mockito:mockito-core:1.+" }
- 交互檢驗(yàn)
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();
- 模擬數(shù)據(jù)
// 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));
- 按順序校驗(yàn)
personDAL.add(any());
personDAL.getAll();
InOrder inOrder = inOrder(personDAL);
inOrder.verify(personDAL).add(any());
inOrder.verify(personDAL).getAll();
- 校驗(yàn)?zāi)硞€(gè)行為沒(méi)有發(fā)生
//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真實(shí)的對(duì)象
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);
注意
不能對(duì)final,Anonymous ,primitive類(lèi)進(jìn)行mock。
設(shè)計(jì)原則(不翻譯,體會(huì)原文)
- Only one type of mock, one way of creating mocks
- No framework-supporting code.
- Slim API.
設(shè)計(jì)之美
我們可以看到mockito設(shè)計(jì)的簡(jiǎn)潔優(yōu)美,以之前的例子為例:
// 設(shè)置mock對(duì)象的行為 - 當(dāng)調(diào)用其get方法獲取第0個(gè)元素時(shí),返回"first"
Mockito.when(mockedList.get(0)).thenReturn("first");
在Mock對(duì)象的時(shí)候,創(chuàng)建一個(gè)proxy對(duì)象,保存被調(diào)用的方法名(get),以及調(diào)用時(shí)候傳遞的參數(shù)(0),然后在調(diào)用thenReturn方法時(shí)再把“first”保存起來(lái),這樣,就有了構(gòu)建一個(gè)stub方法所需的所有信息,構(gòu)建一個(gè)stub。當(dāng)get方法被調(diào)用的時(shí)候,實(shí)際上調(diào)用的是之前保存的proxy對(duì)象的get方法,返回之前保存的數(shù)據(jù)。
具體可以看下面的源碼
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);
}
}
相關(guān)資料
Mockito官網(wǎng)
Mockito文檔
mockito github
mockito作者設(shè)計(jì)理念
反模式的經(jīng)典 - Mockito設(shè)計(jì)解析
Mocks Aren't Stubs
歡迎關(guān)注我的微博