JUnit 5 extension for Mockito

At the time of this writing, there is no official JUnit 5 extension to use Mockito in Jupiter tests. Nevertheless, the JUnit 5 team provides a simple ready to use Java class implementing a simple but effective extension for Mockito. This class can be found in the JUnit 5 user guide (http://junit.org/junit5/docs/current/user-guide/), and its code is the following:

import static org.mockito.Mockito.mock;import java.lang.reflect.Parameter;import org.junit.jupiter.api.extension.ExtensionContext;import org.junit.jupiter.api.extension.ExtensionContext.Namespace;import org.junit.jupiter.api.extension.ExtensionContext.Store;import org.junit.jupiter.api.extension.ParameterContext;import org.junit.jupiter.api.extension.ParameterResolver; ...

Get Mastering Software Testing with JUnit 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.