Mockito provides two primary ways to initialize mock objects in JUnit tests: MockitoAnnotations and MockitoExtension. Each serves a similar purpose but differs in how they are used and integrated into the testing framework.
MockitoAnnotations
**MockitoAnnotations is a class that allows shorthand creation of objects required for testing. It minimizes repetitive mock creation code and makes the test class more readable. To use Mockito annotations like `@Mock`, `@Spy`, `@Captor`, or `@InjectMocks`, you must call `MockitoAnnotations.initMocks(this)` to initialize these annotated fields. This method was deprecated in Mockito 3 and replaced with `MockitoAnnotations.openMocks(this)`, which returns an `AutoCloseable` instance that should be closed after the test to avoid resource leaks[2][5][7].
Here's an example of using `MockitoAnnotations`:
java
public class ArticleManagerTest extends SampleBaseTestCase {
@Mock private ArticleCalculator calculator;
@Mock private ArticleDatabase database;
@Mock private UserProvider userProvider;
private ArticleManager manager;
@Before public void setup() {
MockitoAnnotations.initMocks(this); // or openMocks(this) in Mockito 3
manager = new ArticleManager(userProvider, database, calculator);
}
}
MockitoExtension
**MockitoExtension is a JUnit 5 extension that simplifies the process of using Mockito annotations. By annotating your test class with `@ExtendWith(MockitoExtension.class)`, you can automatically initialize fields annotated with Mockito annotations without needing to manually call `MockitoAnnotations.initMocks(this)` or `MockitoAnnotations.openMocks(this)`. This approach makes the setup more concise and readable[1][3][6].
Here's an example of using `MockitoExtension`:
java
@ExtendWith(MockitoExtension.class)
public class CityServiceImplTest {
@Mock private CityRepository cityRepository;
private CityService cityService;
@BeforeEach
void setUp() {
cityService = new CityServiceImpl(cityRepository);
}
@Test
void testService() {
// Use cityService with mocked cityRepository
}
}
Key Differences
- Initialization: With MockitoAnnotations, you need to manually call `initMocks()` or `openMocks()` to initialize the mocks. In contrast, MockitoExtension automatically initializes the mocks when used with the `@ExtendWith` annotation.
- JUnit Version Compatibility: MockitoAnnotations can be used in both JUnit 4 and JUnit 5, while MockitoExtension is specifically designed for JUnit 5.
- Code Readability and Conciseness: Using MockitoExtension generally results in cleaner and more concise test code, as it eliminates the need for explicit initialization calls.
In summary, MockitoAnnotations provides a flexible way to initialize mocks that works across different JUnit versions but requires manual initialization. MockitoExtension, on the other hand, offers a more streamlined approach specifically for JUnit 5, automating the initialization process and enhancing code readability.
Citations:[1] https://mincong.io/2020/04/19/mockito-junit5/
[2] https://site.mockito.org/javadoc/current/org/mockito/MockitoAnnotations.html
[3] https://reflectoring.io/clean-unit-tests-with-mockito/
[4] https://stackoverflow.com/questions/60308578/what-is-the-difference-between-extendwithspringextension-class-and-extendwit
[5] https://javadoc.io/doc/org.mockito/mockito-core/5.13.0/org/mockito/MockitoAnnotations.html
[6] https://www.vogella.com/tutorials/Mockito/article.html
[7] https://www.arhohuttunen.com/junit-5-mockito/
[8] https://javadoc.io/doc/org.mockito/mockito-core/2.6.5/org/mockito/MockitoAnnotations.html
[9] https://www.lambdatest.com/blog/junit5-mockito-tutorial/
[10] https://symflower.com/en/company/blog/2024/how-to-do-mocking-spring-boot/