如何设置 Mockito 来模拟 Android 单元测试的类

时间:2023-03-29
本文介绍了如何设置 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模板网!

上一篇:使用 Mockito 1.9.5 和 DexMaker-Mockito-1.0 的验证错误 下一篇:使用 mockito 库的 android 应用程序的 Jacoco 代码覆盖

相关文章

最新文章