久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何設置 Mockito 來模擬 Android 單元測試的類

How to set up Mockito to mock class for Android unit test(如何設置 Mockito 來模擬 Android 單元測試的類)
本文介紹了如何設置 Mockito 來模擬 Android 單元測試的類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如果我做一個簡單的測試用例,比如

If I make a simple test case like

@Test
public void myTest() throws Exception {
    Spanned word = new SpannedString("Bird");
    int length = word.length();
}

拋出異常

java.lang.RuntimeException:方法長度在android.text.SpannableStringInternal 沒有被嘲笑.看http://g.co/androidstudio/not-mocked 了解詳情.

java.lang.RuntimeException: Method length in android.text.SpannableStringInternal not mocked. See http://g.co/androidstudio/not-mocked for details.

這在上面的鏈接中解釋為

This is explained in the link above as

用于運行單元測試的 android.jar 文件不包含任何實際代碼 - 由 Android 系統映像提供設備.相反,所有方法都會拋出異常(默認情況下).這是確保您的單元測試只測試您的代碼而不依賴于Android 平臺的任何特定行為(您沒有明確嘲笑,例如使用 Mockito).

The android.jar file that is used to run unit tests does not contain any actual code - that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default). This is to make sure your unit tests only test your code and do not depend on any particular behaviour of the Android platform (that you have not explicitly mocked e.g. using Mockito).

那么如何在 Android 項目中設置 Mockito 以模擬這樣的類?

So how do you set up Mockito in an Android project in order to mock classes like this?

我想學習,所以我將在問答樣式下方添加我的答案.

推薦答案

在你的項目中設置 Mockito 并不難.步驟如下.

It is not difficult to set up Mockito in your project. The steps are below.

假設您使用的是 jcenter 存儲庫(Android Studio 中的默認存儲庫),將以下行添加到您應用的 build.gradle 文件的 dependencies 塊中:

Assuming you are using the jcenter repository (the default in Android Studio), add the following line to the dependencies block of your app's build.gradle file:

testImplementation "org.mockito:mockito-core:2.8.47"

您可以將版本號更新為 最新的 Mockito 版本.

You can update the version number to whatever is the most recent Mockito version is.

它應該看起來像這樣:

dependencies {
    // ...
    testImplementation 'junit:junit:4.12'
    testImplementation "org.mockito:mockito-core:2.8.47"
}

2.將 Mockito 導入您的測試類

通過導入靜態(tài)類,您可以使代碼更具可讀性(即,您可以使用 mock(),而不是調用 Mockito.mock()).

import static org.mockito.Mockito.*;

3.在您的測試中模擬對象

你需要做三件事來模擬對象.

3. Mock objects in your tests

You need to do three things to mock objects.

  1. 使用 mock(TheClassName.class) 創(chuàng)建類的模擬.
  2. 告訴模擬類為您需要調用的任何方法返回什么.您可以使用 whenthenReturn 執(zhí)行此操作.
  3. 在您的測試中使用模擬方法.
  1. Create a mock of the class using mock(TheClassName.class).
  2. Tell the mocked class what to return for any methods you need to call. You do this using when and thenReturn.
  3. Use the mocked methods in your tests.

這是一個例子.真正的測試可能會使用模擬值作為被測試內容的某種輸入.

Here is an example. A real test would probably use the mocked value as some sort of input for whatever is being tested.

public class MyTestClass {

    @Test
    public void myTest() throws Exception {
        // 1. create mock
        Spanned word = mock(SpannedString.class);

        // 2. tell the mock how to behave
        when(word.length()).thenReturn(4);

        // 3. use the mock
        assertEquals(4, word.length());
    }
}

進一步研究

Mockito 還有很多其他功能.請參閱以下資源以繼續(xù)學習.

Further study

There is a lot more to Mockito. See the following resources to continue your learning.

  • Mockito 文檔
  • 使用 Mockito 進行單元測試 - 教程
  • Android 上的 Mockito
  • Jeroen Mols 用 Mockito 做的測試 (YouTube)
  • Mockito documentation
  • Unit tests with Mockito - Tutorial
  • Mockito on Android
  • Testing made sweet with a Mockito by Jeroen Mols (YouTube)

學習 mocking 很好,因為它速度快并且可以隔離正在測試的代碼.但是,如果您正在測試一些使用 Android API 的代碼,那么只使用儀器測試而不是單元測試可能更容易.請參閱此答案.

It is good to learn mocking because it is fast and isolates the code being tested. However, if you are testing some code that uses an Android API, it might be easier to just use an instrumentation test rather than a unit test. See this answer.

這篇關于如何設置 Mockito 來模擬 Android 單元測試的類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
主站蜘蛛池模板: 国产精品一区二区三区99 | 亚洲一区中文字幕 | 一级片在线播放 | 在线视频国产一区 | 中文字幕国产精品 | chinese中国真实乱对白 | 亚洲成人自拍 | 免费看啪啪网站 | 国产精品毛片一区二区三区 | 久久精品二区亚洲w码 | 亚洲欧美激情国产综合久久久 | 一级毛片高清 | 色姑娘综合网 | 男女下面一进一出网站 | 国产一区二区三区视频 | 久久久国产一区二区三区四区小说 | 欧美日韩在线播放 | 精品一区国产 | 国产在线精品一区二区三区 | 国产一区二区三区在线 | 精品国产青草久久久久福利 | 亚洲精品久久久久久久久久久久久 | 午夜一区 | 一区二区三区国产精品 | 午夜精品一区二区三区免费视频 | 欧美日韩精品免费 | 日韩精品区 | 在线婷婷| 色一情一乱一伦一区二区三区 | 国产精品久久久久久久午夜片 | 色婷婷综合成人av | 中文字幕日韩欧美一区二区三区 | 免费1区2区3区 | 天天色天天射天天干 | 在线日韩av电影 | 亚洲成人一区 | 国产精品美女久久久久aⅴ国产馆 | 欧美日韩在线综合 | 黄色网址在线播放 | 亚洲一区二区中文字幕 | 伊人艹 |