如果我做一个简单的测试用例,比如
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"
}
通过导入静态类,您可以使代码更具可读性(即,您可以使用 mock()
,而不是调用 Mockito.mock()
).
import static org.mockito.Mockito.*;
你需要做三件事来模拟对象.
You need to do three things to mock objects.
mock(TheClassName.class)
创建类的模拟.when
和 thenReturn
执行此操作.mock(TheClassName.class)
.when
and thenReturn
. 这是一个例子.真正的测试可能会使用模拟值作为被测试内容的某种输入.
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 还有很多其他功能.请参阅以下资源以继续学习.
There is a lot more to Mockito. See the following resources to continue your learning.
学习 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模板网!