久久久久久久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 導入您的測試類

通過導入靜態類,您可以使代碼更具可讀性(即,您可以使用 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) 創建類的模擬.
  2. 告訴模擬類為您需要調用的任何方法返回什么.您可以使用 whenthenReturn 執行此操作.
  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 還有很多其他功能.請參閱以下資源以繼續學習.

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 中的數值?)
主站蜘蛛池模板: 成人免费视频观看视频 | 波多野结衣二区 | 狠狠的干狠狠的操 | 91久久国产 | 欧美日韩一区不卡 | 国产91 在线播放 | 国产免费一区二区 | 天天拍天天操 | 毛片免费看的 | 亚洲国产成人精品女人久久久 | 久久国产精品99久久久久 | 操夜夜 | 国产精品一区二区久久 | 日韩av成人在线 | 电影午夜精品一区二区三区 | 91视频国产精品 | 久草欧美| 91在线视频免费观看 | 亚洲精品免费视频 | 国产免费一区二区 | 国产中文区二幕区2012 | 亚洲成人精品一区二区 | 欧美成视频 | 四虎影院一区二区 | 欧美区日韩区 | 久精品久久 | 九九热国产精品视频 | 欧美一区在线视频 | 亚洲 精品 综合 精品 自拍 | 国产欧美在线观看 | 91久久精品国产 | 亚洲天堂影院 | 成人欧美一区二区三区黑人孕妇 | 日韩一二区 | 成人精品一区二区户外勾搭野战 | 免费看淫片 | 国产亚洲精品久久久久久豆腐 | 日韩精品免费视频 | 一级在线观看 | julia中文字幕久久一区二区 | 中文字幕在线视频一区二区三区 |