Mockito Setup Spring Boot | Java Fresher Interview
Fast recall
Quick revision
Core answer
spring-boot-starter-test normally supplies Mockito and its JUnit Jupiter integration. Without that starter, add the Mockito dependencies with test scope. A mock can then be created directly with Mockito.mock(...) or through @Mock in a test managed by MockitoExtension.Interview-ready guide
Interview answer
Mockito is a Java library for creating controlled test doubles.
I use it when the class under test depends on something such as a repository or remote client and I want the test to run without that real system. The test creates a mock, stubs only the reply needed for the case, calls the real service, and asserts the result. It can also verify an interaction that is part of the behavior.
In Spring Boot, Mockito normally comes from the declared spring-boot-starter-test dependency. In a plain project I can add mockito-core, plus mockito-junit-jupiter when I want MockitoExtension to initialize @Mock fields. This is still an ordinary unit-test setup; replacing a bean in a Spring context is a separate feature.
Mockito Setup Spring Boot | Java Fresher Interview
Direct Answer
Mockito is a Java mocking library. It creates test doubles for collaborators such as repositories or HTTP clients, lets a test define their replies, and records calls for verification. In a Spring Boot project, declaring spring-boot-starter-test normally supplies Mockito and its JUnit Jupiter integration. Without that starter, add the Mockito dependencies with test scope. A mock can then be created directly with Mockito.mock(...) or through @Mock in a test managed by MockitoExtension.
Code Example
@ExtendWith(MockitoExtension.class)
class PriceServiceTest {
@Mock ExchangeRateClient rates;
@Test
void convertsEurosToRupees() {
when(rates.eurToInr()).thenReturn(new BigDecimal("90.00"));
var service = new PriceService(rates);
assertEquals(new BigDecimal("900.00"), service.inRupees(new BigDecimal("10.00")));
verify(rates).eurToInr();
}
}
The mock avoids a real network call. The stub supplies a known exchange rate, the assertion checks the service result, and verify confirms the required collaboration.
Common Mistakes & Pitfalls
- Assuming Mockito is automatically present: verify the declared test dependencies.
- Starting Spring for an ordinary unit test: construct the subject directly when only Java behavior is under test.
- Mocking the class under test: mock its external collaborators and exercise the real subject.
- Verifying calls without checking the result: assert the observable behavior first; verify only meaningful interactions.
- Mixing version management: prefer Boot-managed versions unless the build has a deliberate, tested override.
Check your understanding
Answer these without looking back at the article.
- What is MockitoAnnotations.openMocks(this)?
- What is mockito-inline and when do you need it?
- How do you mock a static method with Mockito?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.