日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mockito简要教程

發布時間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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是一個非常不錯的模擬框架。 它使您可以使用干凈簡單的API編寫漂亮的測試。 Mockito不會給您帶來麻煩,因為這些測試可讀性強,并且會產生清晰的驗證錯誤。

官方說明-how簡要版本

mockito官方文檔-詳細版本

特性和動機github說明

其特性如下:

  • mock具體的類和接口;
  • 小注釋語法糖-@Mock
  • 驗證錯誤是干凈的-單擊堆棧跟蹤以查看測試中失敗的驗證; 單擊異常原因以導航到代碼中的實際交互。 堆棧跟蹤始終是干凈的。
  • 允許按順序進行靈活的驗證(例如:按順序進行驗證,而不是每次交互都進行驗證)
  • 支持精確次數和最少一次的驗證
  • 使用參數匹配器(anyObject(),anyString()或refEq()進行基于反射的相等匹配)的靈活驗證或存根
  • 允許創建自定義參數匹配器或使用現有的Hamcrest匹配器

2 mockito應用

gradle倉庫添加相關配置如下:

repositories { jcenter() } dependencies { testCompile "org.mockito:mockito-core:2.+" }

之于maven的相關配置,可以搜索添加pom的相關依賴;

2.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();

2.2 存根方法調用

// 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 回調

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.* 更多

更多說明參加詳細資料文檔mockito官方文檔-詳細版本

3 verify

Mockito提供vertify關鍵字來實現校驗方法是否被調用,其具體作用可總結如下:

  • 測試方法是否被調用;
  • vervify(mock,times)的具體調用次數;
@Testpublic 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));//驗證是否執行過一次updateverify(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:

4 mock和spy的區別

項目中,有些函數需要處理某個服務的返回結果,而在對函數單元測試的時候,又不能啟動那些服務,這里就可以利用Mockito工具。Mockito中的Mock和Spy都可用于攔截那些尚未實現或不期望被真實調用的對象和方法,并為其設置自定義行為。二者的區別在于:

1、Mock聲明的對象,對函數的調用均執行mock(即虛假函數),不執行真正部分。

2、Spy聲明的對象,對函數的調用均執行真正部分。

5 mockito的使用建議

這是官方網站上給出的一些mockito使用備忘:

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

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

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

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。