如何将Mockito设置为模拟类以进行Android单元测试 [英] How to set up Mockito to mock class for Android unit test

查看:180
本文介绍了如何将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导入测试类

通过导入静态类,可以使代码更具可读性(即,可以不使用Mockito.mock()而是使用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 documentation
  • Unit tests with Mockito - Tutorial
  • Mockito on Android
  • Testing made sweet with a Mockito by Jeroen Mols (YouTube)

学习模拟是件好事,因为它速度快并且可以隔离正在测试的代码.但是,如果您要测试一些使用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单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆