Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Introduction

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.

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

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.

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

2. Why Is This Warning Shown

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

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:

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:

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.

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

3. Solutions

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

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

import org.mockito.junit.MockitoJUnitRunner;

public class ExampleTest {

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

3.2. Initializing Fields Using MockitoAnnotations

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

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

public class ExampleTest {
    public void initMocks() {


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

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

3.3. Using 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:

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

public class ExampleTest {

    public MockitoRule rule = MockitoJUnit.rule();


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

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

4. Conclusion

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


Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!