JUnit Mockito Integration
JUnit Mockito integration connects Mockito's mock annotations to the JUnit 5 test lifecycle. The `MockitoExtension` initializes annotated mocks before each test and handles Mockito-specific lifecycle behavior.
With JUnit 5, Mockito can be integrated through `@ExtendWith(MockitoExtension.class)`. This tells JUnit to use Mockito's extension during test execution, so fields annotated with `@Mock`, `@Spy`, `@Captor`, and related annotations can be initialized automatically.
Without the extension, an annotation such as `@Mock` does not by itself guarantee that Mockito creates the mock during a normal JUnit 5 test. An alternative is explicit initialization with `MockitoAnnotations.openMocks(this)`, but the JUnit extension is usually simpler for tests that use Mockito annotations.
A typical unit test declares dependencies as `@Mock` fields and creates the class under test with `@InjectMocks`. Mockito then supplies the mock dependencies when the test instance is initialized. `@InjectMocks` is convenient, but it does not replace understanding how the class's dependencies are constructed.
The extension also participates in Mockito's test lifecycle, helping Mockito manage resources and validate certain mocking behavior around test execution. This keeps setup code focused on the test rather than on manually starting Mockito for every test class.
The integration is specifically about connecting two frameworks: JUnit controls test discovery and execution, while Mockito provides mock objects and interaction-based testing facilities.
Key points
- Use `@ExtendWith(MockitoExtension.class)` to integrate Mockito with JUnit 5.
- Mockito annotations such as `@Mock` are initialized through the extension.
- `@InjectMocks` can create the class under test and inject available Mockito mocks.
- JUnit manages execution; Mockito manages mocks and verification.
- Manual `MockitoAnnotations.openMocks(this)` is an alternative initialization approach.
Interview questions
What does `@ExtendWith(MockitoExtension.class)` do?
It registers Mockito's JUnit 5 extension for the test class. The extension initializes Mockito annotations such as `@Mock` before tests execute. It also integrates Mockito's lifecycle handling with JUnit. This avoids manually initializing annotated mocks in each test.
What is the difference between `@Mock` and `@InjectMocks`?
`@Mock` tells Mockito to create a mock object for a dependency. `@InjectMocks` tells Mockito to create an instance of the class under test and inject suitable Mockito-created mocks into it. The injection is performed using Mockito's supported constructor, setter, or field injection strategies. It is not equivalent to starting a dependency-injection framework such as Spring.
How can Mockito annotations be initialized without `MockitoExtension`?
A test can call `MockitoAnnotations.openMocks(this)` to initialize Mockito annotations explicitly. This is commonly done from a setup method that runs before tests. The returned resource should be managed appropriately, especially when the Mockito version and configuration require cleanup. The JUnit 5 extension generally provides a cleaner lifecycle integration.
Why would you use Mockito with JUnit for a unit test instead of loading the Spring context?
Mockito can isolate the class under test from external collaborators by replacing those collaborators with test doubles. That usually keeps a focused unit test independent of application infrastructure. A Spring context test exercises framework wiring and therefore answers a different testing question. The choice depends on whether the goal is isolated unit behavior or integration behavior.
What role does the Mockito extension play in the JUnit 5 lifecycle?
The extension hooks Mockito into JUnit 5's extension lifecycle. It can initialize annotated Mockito fixtures before each test and perform Mockito-specific lifecycle processing around test execution. This keeps mock management aligned with JUnit's test instances and execution model. It also avoids scattering manual initialization and cleanup logic throughout the test class.