Introduction
In this chapter, we will learn about using the @ExtendWith(MockitoExtension.class) annotation in Mockito. This annotation is used to enable Mockito in JUnit 5 tests, making it easier to initialize mocks, spies, and other Mockito features. The MockitoExtension class is part of the Mockito library and integrates with JUnit 5’s extension model to provide seamless initialization of mock objects.
Key Points about @ExtendWith(MockitoExtension.class)
- Enables Mockito in JUnit 5: Integrates Mockito with JUnit 5.
- Automatic Initialization: Automatically initializes mocks, spies, and other Mockito features.
- Simplifies Test Setup: Reduces boilerplate code for setting up tests.
- Managed by Mockito: The initialization of mocks is managed by Mockito.
Setting Up Mockito and JUnit 5
Ensure that you have the necessary dependencies in your pom.xml if you are using Maven:
<dependencies>
<!-- Mockito Core dependency -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- Mockito JUnit Jupiter dependency -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- JUnit 5 dependency -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Example: Using @ExtendWith(MockitoExtension.class)
Description
In this example, we will create a UserService class that depends on a UserRepository class. The UserService class will use the UserRepository class to fetch user details. We will create unit tests for the UserService class using Mockito’s @ExtendWith(MockitoExtension.class) annotation to initialize the mock UserRepository.
Class Under Test: UserService
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User getUserDetails(String username) {
return userRepository.findByUsername(username);
}
}
Supporting Class: UserRepository
public interface UserRepository {
User findByUsername(String username);
}
Entity Class: User
public class User {
private String username;
private String firstName;
private String lastName;
// getters and setters
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Test Class: UserServiceTest
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(MockitoExtension.class)
public class UserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;
@Test
void testGetUserDetails() {
// Arrange
User user = new User();
user.setUsername("johndoe");
user.setFirstName("John");
user.setLastName("Doe");
when(userRepository.findByUsername("johndoe")).thenReturn(user);
// Act
User result = userService.getUserDetails("johndoe");
// Assert
assertNotNull(result);
assertEquals("John", result.getFirstName());
assertEquals("Doe", result.getLastName());
}
}
Explanation
- ExtendWith Annotation:
@ExtendWith(MockitoExtension.class)The
@ExtendWith(MockitoExtension.class)annotation integrates Mockito with JUnit 5. It ensures that Mockito features like mocks and spies are automatically initialized. - Mock Annotation:
@Mock private UserRepository userRepository;The
@Mockannotation creates a mock object for theUserRepositoryclass. - InjectMocks Annotation:
@InjectMocks private UserService userService;The
@InjectMocksannotation creates an instance of theUserServiceclass and injects theUserRepositorymock into it. - Test Method:
@Test void testGetUserDetails() { // Arrange User user = new User(); user.setUsername("johndoe"); user.setFirstName("John"); user.setLastName("Doe"); when(userRepository.findByUsername("johndoe")).thenReturn(user); // Act User result = userService.getUserDetails("johndoe"); // Assert assertNotNull(result); assertEquals("John", result.getFirstName()); assertEquals("Doe", result.getLastName()); }This test method sets up the mock behavior (Arrange), calls the method under test (Act), and checks the expected results (Assert). It verifies that the
UserRepositorymock is used by theUserServiceto fetch user details.
Output

Conclusion
The @ExtendWith(MockitoExtension.class) annotation in Mockito simplifies the setup of tests by integrating Mockito with JUnit 5. It ensures that mocks, spies, and other Mockito features are automatically initialized, reducing boilerplate code and making tests cleaner and more maintainable.