Mockito Setup in Spring Boot
Fast recall
Quick revision
Core answer
spring-boot-starter-test normally receives Mockito and its JUnit Jupiter support through that test dependency. For a plain JUnit 5 unit test, use @ExtendWith(MockitoExtension.class) or create mocks with Mockito.mock. Add a direct Mockito dependency only when the project does not use the Boot test starter or needs an explicitly managed setup.Interview-ready guide
Interview answer
Mockito is a Java library for replacing a dependency with a controlled test double. In a Spring Boot build, it is usually available through the declared spring-boot-starter-test dependency, which Boot version-manages with JUnit Jupiter and other test libraries. I still check the build file rather than assuming every project includes that starter. For a plain unit test I use @ExtendWith(MockitoExtension.class), declare collaborators with @Mock, and construct the real service directly or use @InjectMocks for simple wiring. I can also call Mockito.mock(...) without annotations. A Spring context is not needed for this setup. If the test genuinely needs to replace a Spring-managed bean, current Spring uses @MockitoBean; older Boot projects may still use deprecated @MockBean.
Mockito Setup in Spring Boot
Direct Answer
Mockito creates configurable test doubles for Java. A Spring Boot project that declares spring-boot-starter-test normally receives Mockito and its JUnit Jupiter support through that test dependency. For a plain JUnit 5 unit test, use @ExtendWith(MockitoExtension.class) or create mocks with Mockito.mock. Add a direct Mockito dependency only when the project does not use the Boot test starter or needs an explicitly managed setup.
Code Example
<!-- Maven: the declared Boot test starter supplies Mockito support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
// Plain unit test: no Spring context
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock OrderRepository repository;
@Test
void findsAnOrder() {
var service = new OrderService(repository);
when(repository.findById(7L)).thenReturn(Optional.of(new Order(7L)));
assertEquals(7L, service.find(7L).id());
}
}
// Context test: current Spring bean override
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@MockitoBean OrderService service;
}
Common Mistakes & Pitfalls
- Assuming the starter exists: confirm it in the build file.
- Adding competing versions: let Spring Boot manage versions unless the project has a deliberate override.
- Forgetting Mockito initialization:
@Mockfields need the extension, runner, oropenMockslifecycle. - Confusing unit and context tests: a plain Mockito test does not need
@SpringBootTest. - Using a legacy bean override in new code: current Spring uses
@MockitoBean; Boot's@MockBeanis deprecated since 3.4.
Check your understanding
Answer these without looking back at the article.
- What is the difference between @Mock and @MockBean?
- Why would you prefer @ExtendWith(MockitoExtension.class) over MockitoAnnotations.openMocks()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.