問題描述
如果我做一個簡單的測試用例,比如
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.
- 使用
mock(TheClassName.class)
創建類的模擬. - 告訴模擬類為您需要調用的任何方法返回什么.您可以使用
when
和thenReturn
執行此操作. - 在您的測試中使用模擬方法.
- Create a mock of the class using
mock(TheClassName.class)
. - Tell the mocked class what to return for any methods you need to call. You do this using
when
andthenReturn
. - 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模板網!