Warning: “The type MockitoJUnitRunner is deprecated” – 警告:“MockitoJUnitRunner类型已被废弃&#8221

最后修改: 2018年 9月 24日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.简介

In this quick tutorial, we’ll have a look at one of the warnings we may see when working with the popular testing framework Mockito.

在这个快速教程中,我们将看看在使用流行的测试框架Mockito时可能看到的一个警告。

Namely, the one referring to the deprecated MockitoJUnitRunner class. We’ll see why this warning happens and how to handle it.

也就是提到被废弃的MockitoJUnitRunner类的那个。我们将看看为什么会发生这种警告以及如何处理它。

Finally, let’s remind that we can use MockitoJUnitRunner to instruct Mockito to initialize our test-doubles annotated with @Mock or @Spy, along with other Mockito annotations.

最后,让我们提醒一下,我们可以使用MockitoJUnitRunner来指示Mockito初始化我们用@Mock@Spy注解的测试二元组,以及其他Mockito注解。

To learn more about testing with Mockito, check out our Mockito series here.

要了解有关使用Mockito进行测试的更多信息,请查看我们的Mockito系列,这里

2. Why Is This Warning Shown

2.为什么会出现这种警告

This deprecation warning will appear if we’re using a version of Mockito before 2.2.20 (November 2016).

如果我们使用的是2.2.20(2016年11月)之前的Mockito版本,就会出现这个弃用警告。

Let’s briefly go through the history behind it. In earlier versions of Mockito, if we wanted to use the Mockito JUnit Runner the package we needed to import was:

让我们简单地回顾一下它背后的历史。在Mockito的早期版本中,如果我们想使用Mockito JUnit Runner,我们需要导入的包是。

import org.mockito.runners.MockitoJUnitRunner;

From version 2.2.20 onwards JUnit related classes have been regrouped into a specific JUnit package. We can find the package here:

从2.2.20版本开始,JUnit相关的类被重新组合到一个特定的JUnit包中。我们可以在这里找到这个包。

import org.mockito.junit.MockitoJUnitRunner;

Consequently, the original org.mockito.runners.MockitoJUnitRunner is now deprecated. The class’s logic now belongs to org.mockito.junit.runners.MockitoJUnitRunner.

因此,原来的org.mockito.runners.MockitoJUnitRunner现在已经被废弃。该类的逻辑现在属于org.mockito.junit.runners.MockitoJUnitRunner

While removing the warning is not mandatory, it’s recommended to do so. Mockito version 3 will remove this class.

虽然删除警告并不是强制性的,但还是建议这样做。Mockito第3版将删除这个类。

3. Solutions

3.解决方案

In this section we’ll explain three different solutions for resolving this deprecation warning:

在本节中,我们将解释三种不同的解决方案来解决这个废弃警告。

  • Updating to use the correct import
  • Initialising fields using MockitoAnnotations
  • Using MockitoRule

3.1. Updating Imports

3.1.更新进口产品

Let’s begin with the simplest solution which is to simply change the package import statement:

让我们从最简单的解决方案开始,即简单地改变包的导入语句

import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ExampleTest {
    //...
}

And that’s all! The change should be fairly easy to make.

这就是全部!这种改变应该是相当容易做到的。

3.2. Initializing Fields Using MockitoAnnotations

3.2.使用MockitoAnnotations初始化字段

In this next example, we’ll initialize our mocks a different way using MockitoAnnotations class:

在接下来的例子中,我们将使用MockitoAnnotations类以不同的方式初始化我们的mocks

import org.junit.Before;
import org.mockito.MockitoAnnotations;

public class ExampleTest {
    
    @Before 
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    //...
}

First of all, we remove the reference to MockitoJUnitRunner. Instead, we call the static initMocks() method of the MockitoAnnotations class.

首先,我们删除对MockitoJUnitRunner的引用。相反,我们调用MockitoAnnotations类的静态initMocks()方法。

We do this in JUnit @Before method of test’s class. This initializes any fields with Mockito annotations before each test is executed.

我们在JUnit测试类的@Before方法中完成这个工作。这将在每次测试执行前初始化任何带有Mockito注释的字段。

3.3. Using MockitoRule

3.3.使用MockitoRule

However, as we’ve already mentioned, MockitoJUnitRunner is by no means mandatory. In this last example, we’ll look at another way we can get @Mock working using MockitoRule:

然而,正如我们已经提到的,MockitoJUnitRunner绝不是强制性的。在这最后一个例子中,我们将看看另一种方法,我们可以使用@Mock工作,MockitoRule:

import org.junit.Rule;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

public class ExampleTest {

    @Rule
    public MockitoRule rule = MockitoJUnit.rule();

    //...
}

Finally, in this example, the JUnit rule initializes any mocks annotated with @Mock.

最后,在这个例子中,JUnit规则初始化了任何用@Mock注释的mock。

Hence, this means that the explicit usage of MockitoAnnotations#initMocks(Object) or @RunWith(MockitoJUnitRunner.class) is not necessary.

因此,这意味着不需要明确使用MockitoAnnotations#initMocks(Object)@RunWith(MockitoJUnitRunner.class)

4. Conclusion

4.结论

To summarize, in this short article we saw several options on how to fix the MockitoJUnitRunner class deprecation warning.

总结一下,在这篇短文中,我们看到了关于如何修复MockitoJUnitRunner类废弃警告的几种方案。