Introduction
Mockito is the most popular library for creating mocks in Java. It allows you to isolate the tested classes from their external dependencies. In this tutorial, you will learn how to configure Mockito, create mocks, and write clear assertions. Unit tests thus become faster and more reliable. Each concept is illustrated with complete code that you can copy directly.
Prerequisites
- JDK 17 or higher
- Maven or Gradle
- Basic knowledge of Java and JUnit 5
- IDE such as IntelliJ IDEA or VS Code
Add the Maven Dependency
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>These dependencies add Mockito and its JUnit 5 integration. The test scope ensures they are only present during the testing phase.
Create the Class to Test
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
public String getUserName(Long id) {
return repository.findById(id).getName();
}
}This simple class depends on a repository. We will test it by mocking the repository to avoid any database access.
Write the Test with @Mock
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;
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Mock
private UserRepository repository;
@InjectMocks
private UserService service;
@Test
void shouldReturnUserName() {
// test to complete
}
}The @ExtendWith annotation enables Mockito. @Mock creates a mock and @InjectMocks automatically injects this mock into UserService.
Configure Mock Behavior
import static org.mockito.Mockito.when;
@Test
void shouldReturnUserName() {
User user = new User(1L, "Alice");
when(repository.findById(1L)).thenReturn(user);
String name = service.getUserName(1L);
assertEquals("Alice", name);
}The when(...).thenReturn(...) method defines the mock's behavior. The test becomes predictable and independent of the database.
Verify Interactions
import static org.mockito.Mockito.verify;
@Test
void shouldCallRepositoryOnce() {
service.getUserName(1L);
verify(repository).findById(1L);
}verify() ensures the mock was called with the correct arguments. This is essential for testing behavior.
Best Practices
- Use @Mock and @InjectMocks for better readability
- Name your tests clearly with should...
- Verify only important interactions
- Avoid overly complex mocks that hide real logic
- Keep your tests independent from each other
Common Mistakes to Avoid
- Forgetting @ExtendWith(MockitoExtension.class) causes NullPointerException
- Calling verify() before executing the tested method
- Using mocks to test internal logic instead of isolating dependencies
- Not resetting mocks between tests
Going Further
Check out our Learni courses to dive deeper into advanced testing with Mockito and Spring Boot.