日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

mockito简要教程

發(fā)布時(shí)間:2023/12/15 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mockito简要教程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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ù);
@Testpublic void update() throws Exception {boolean result = personService.update(1, "new name");//驗(yàn)證mockDao的getPeron從未被調(diào)用verify(mockDao,never()).getPerson(1);assertTrue("must true", result);//驗(yàn)證是否執(zhí)行過一次getPerson(1)verify(mockDao, times(1)).getPerson(eq(1));//驗(yàn)證是否執(zhí)行過一次updateverify(mockDao, times(1)).update(isA(Person.class)); }

但是需要注意的是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使用備忘:

  • 不要mock你不曾擁有的類型;
  • 不要試圖mock值對象;
  • 不要mock所有的東西;
  • 用測試表達(dá)愛意;
  • 以下是本人的一些感悟:

  • 好的面向的對象的業(yè)務(wù)代碼對于mock測試代碼的編寫也是有幫助的,反向的壞過程的代碼會(huì)增加你寫mock代碼的過程。因此我們寫一個(gè)邏輯的時(shí)候–最好多想想如何好用也好測–這對于函數(shù)層次結(jié)構(gòu)、入?yún)鬟f都是有很多需要注意的事項(xiàng);
  • 對于對值結(jié)果要求比較多的測試內(nèi)容,最好直接構(gòu)造你的預(yù)期結(jié)構(gòu)而不是mock,mock適合測試流程,但不是所有的東西!
  • 一開始就學(xué)著去寫,而不是寫完了測完了再去補(bǔ),否則你寫單元測試的目的完全沒有達(dá)到。我們寫單元測試從一開始就是希望呢能在一個(gè)不那么嚴(yán)格的環(huán)境里面把自己的邏輯測到!
  • 函數(shù)不要嵌套太深,學(xué)會(huì)抽象公共方法;
  • 特別注意靜態(tài)工具類方法的使用,用的不好可能會(huì)加大自己寫測試代碼的復(fù)雜度。
  • 總結(jié)

    以上是生活随笔為你收集整理的mockito简要教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。