To mock dependencies in unit tests, you usually use a mocking framework such as Mockito. Mocking lets you test one class in isolation without running the real logic of its collaborators.
Basic Mockito Example
Suppose you have a service that depends on another class:
@Service
public class MyService {
private final MyDependency dependency;
public MyService(MyDependency dependency) {
this.dependency = dependency;
}
public void process() {
dependency.doSomething();
}
}
You can mock MyDependency in a unit test like this:
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.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
class MyServiceTest {
@Mock
private MyDependency dependency;
@InjectMocks
private MyService myService;
@Test
void processCallsDependency() {
myService.process();
verify(dependency).doSomething();
}
}
What the annotations mean
@Mockcreates a mock object.@InjectMockscreates the class under test and injects the mocks into it.@ExtendWith(MockitoExtension.class)enables Mockito support in JUnit 5.verify(...)checks that a method was called.
Mocking return values
If the dependency returns a value, use when(...).thenReturn(...):
import static org.mockito.Mockito.when;
when(repository.findNameById(1L)).thenReturn("Alice");
Example:
@Test
void returnsMockedValue() {
when(userRepository.findNameById(1L)).thenReturn("Alice");
String result = userService.getUserName(1L);
assertEquals("Alice", result);
}
Mocking exceptions
You can also make a mock throw an exception:
import static org.mockito.Mockito.when;
when(repository.findById(99L))
.thenThrow(new EntityNotFoundException("User not found"));
For void methods, use doThrow(...):
import static org.mockito.Mockito.doThrow;
doThrow(new RuntimeException("Failure"))
.when(dependency)
.doSomething();
Verifying interactions
You can verify how your class interacted with its dependencies:
verify(dependency).doSomething();
verify(dependency, times(1)).doSomething();
verify(dependency, never()).doSomething();
Spring Boot unit test example
For a pure unit test, prefer Mockito without starting the Spring context:
@ExtendWith(MockitoExtension.class)
class MyServiceTest {
@Mock
private MyDependency dependency;
@InjectMocks
private MyService service;
@Test
void processCallsDependency() {
service.process();
verify(dependency).doSomething();
}
}
Spring integration-style test
If you need the Spring context, use Spring’s test support and replace a bean with a mock:
@SpringBootTest
class MyServiceSpringTest {
@MockitoBean
private MyDependency dependency;
@Autowired
private MyService service;
@Test
void processCallsDependency() {
service.process();
verify(dependency).doSomething();
}
}
Use this style when you want to test Spring wiring, configuration, transactions, security, or other framework behavior.
Rule of thumb
- Use Mockito
@Mock+@InjectMocksfor fast unit tests. - Use Spring test annotations only when you need the Spring application context.
- Mock external systems such as databases, APIs, message queues, and file systems.
- Avoid mocking simple value objects or the class you are actually testing.
