
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: January 8, 2024
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.
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.
In this section we’ll explain three different solutions for resolving this deprecation warning:
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.
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 {
@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.
We do this in JUnit @Before method of test’s class. This initializes any fields with Mockito annotations before each test is executed.
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 {
@Rule
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.
To summarize, in this short article we saw several options on how to fix the MockitoJUnitRunner class deprecation warning.