mockito简要教程
1 mockito概述
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是一個(gè)非常不錯(cuò)的模擬框架。 它使您可以使用干凈簡單的API編寫漂亮的測試。 Mockito不會(huì)給您帶來麻煩,因?yàn)檫@些測試可讀性強(qiáng),并且會(huì)產(chǎn)生清晰的驗(yàn)證錯(cuò)誤。
官方說明-how簡要版本
mockito官方文檔-詳細(xì)版本
特性和動(dòng)機(jī)github說明
其特性如下:
- mock具體的類和接口;
- 小注釋語法糖-@Mock
- 驗(yàn)證錯(cuò)誤是干凈的-單擊堆棧跟蹤以查看測試中失敗的驗(yàn)證; 單擊異常原因以導(dǎo)航到代碼中的實(shí)際交互。 堆棧跟蹤始終是干凈的。
- 允許按順序進(jìn)行靈活的驗(yàn)證(例如:按順序進(jìn)行驗(yàn)證,而不是每次交互都進(jìn)行驗(yàn)證)
- 支持精確次數(shù)和最少一次的驗(yàn)證
- 使用參數(shù)匹配器(anyObject(),anyString()或refEq()進(jìn)行基于反射的相等匹配)的靈活驗(yàn)證或存根
- 允許創(chuàng)建自定義參數(shù)匹配器或使用現(xiàn)有的Hamcrest匹配器
2 mockito應(yīng)用
gradle倉庫添加相關(guān)配置如下:
repositories { jcenter() } dependencies { testCompile "org.mockito:mockito-core:2.+" }之于maven的相關(guān)配置,可以搜索添加pom的相關(guān)依賴;
2.1 驗(yàn)證互動(dòng)
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();2.2 存根方法調(diào)用
// 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));2.3 mock注解使用
public class ArticleManagerTest {@Mock private ArticleCalculator calculator;@Mock private ArticleDatabase database;@Mock private UserProvider userProvider;private ArticleManager manager;2.4 回調(diào)
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"));2.* 更多
更多說明參加詳細(xì)資料文檔mockito官方文檔-詳細(xì)版本
3 verify
Mockito提供vertify關(guān)鍵字來實(shí)現(xiàn)校驗(yàn)方法是否被調(diào)用,其具體作用可總結(jié)如下:
- 測試方法是否被調(diào)用;
- vervify(mock,times)的具體調(diào)用次數(shù);
但是需要注意的是verify的使用限制于mack對象,對于普通對象不支持相關(guān)檢測,否則會(huì)觸發(fā)相關(guān)錯(cuò)誤。
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:4 mock和spy的區(qū)別
項(xiàng)目中,有些函數(shù)需要處理某個(gè)服務(wù)的返回結(jié)果,而在對函數(shù)單元測試的時(shí)候,又不能啟動(dòng)那些服務(wù),這里就可以利用Mockito工具。Mockito中的Mock和Spy都可用于攔截那些尚未實(shí)現(xiàn)或不期望被真實(shí)調(diào)用的對象和方法,并為其設(shè)置自定義行為。二者的區(qū)別在于:
1、Mock聲明的對象,對函數(shù)的調(diào)用均執(zhí)行mock(即虛假函數(shù)),不執(zhí)行真正部分。
2、Spy聲明的對象,對函數(shù)的調(diào)用均執(zhí)行真正部分。
5 mockito的使用建議
這是官方網(wǎng)站上給出的一些mockito使用備忘:
以下是本人的一些感悟:
總結(jié)
以上是生活随笔為你收集整理的mockito简要教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UV-LED点光源,厂家,UVLED点光
- 下一篇: 点亮led灯的个数_LED灯的点亮电流一