Using Mockito annotations in JUnit 5

Default featured post

Using Mockito annotations in JUnit 5. The latest release of JUnit is not backward compatible and does not work straightforwardly with many testing frameworks such as Mockito. Mockito annotations do not work in JUnit 5 by default. It needs some tweaks to make it work. In this article, we cover how to make Mockito annotations work with JUnit 5.

Assuming that we have a test class written in JUnit 4 and we want to convert it to JUnit 5.

The test can be converted to JUnit 5 in two ways.

Injecting the dependencies manually to the class under the test

As you can, in this approach no Mockito annotation is used and all dependencies are injected into the class manually using the constructor injection.

The problems with this approach are:

  1. The class under the test should support constructor dependency injection. It does not work with the field dependency injection.
  2. If a new dependency added, the code will not compile.
  3. Every time a test case runs, a new instance of the class is created.
  4. Much of the test class need to be changed.

Hence a better way is to keep using Mockito annotations.

Adding mockito-junit-jupiter package and using annotations

Mockito has a JUnit 5 compatibility package that allows the tests written in JUnit 5 to work with Mockito annotations easily. All that needed is to add an extra dependency to the project like this:

As you can see beside mockito-code, there is a mockito-junit-jupiter dependency which helps us to leverage on Mockito annotations in JUnit 5.

After adding the new Mockito dependency the next step is to rewrite the code in JUnit 5. The final code will look like this:

As you can see, we replaced @RunWith with @ExtendWith as the former does not exist in JUnit 5 anymore. Then we passed MockitoExtension.class to the annotation which is coming from mockito-junit-jupiter package. And lastly, replaced @Test with the JUnit 5 counterpart.

Happy testing!