Unit Test Spring Service Mockito | Java Fresher Interview
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
To write a unit test for a Spring service using Mockito, you start by annotating the test class with at-ExtendWith MockitoExtension dot class.
That's the JUnit 5 way to activate Mockito. Then you create mocks for the service's dependencies using at-Mock.
For example, if your OrderService depends on an OrderRepository, you'd write at-Mock private OrderRepository orderRepository. Next, you annotate the service itself with at-InjectMocks, which tells Mockito to create a real OrderService instance and inject the mock repository into it. Inside your test method, you use when and thenReturn to define the mock's behavior.
Say when orderRepository dot save is called with any Order, return a saved Order with an ID. Then you call your service method and use JUnit assertions to verify the result. The key thing here is this runs without Spring—no application context, no database connection, nothing.
It's pure Java object testing. That's why it's so fast. You can run hundreds of these tests in a couple seconds.
One mistake I see freshers make is adding at-SpringBootTest to these tests. That loads the entire Spring application, which takes several seconds per test. For unit testing service logic, you don't need Spring at all.
Save at-SpringBootTest for end-to-end integration tests where you're testing the whole application flow.
Unit Test Spring Service Mockito | Java Fresher Interview
Direct Answer
Use @ExtendWith(MockitoExtension.class) to run the test with Mockito, @Mock to create a mock repository, and @InjectMocks to inject that mock into your service. This is a true unit test—no Spring context loads, runs in milliseconds. Only use @SpringBootTest when you need the full application context for integration testing.
Code Example
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock
private OrderRepository orderRepository;
@InjectMocks
private OrderService orderService;
@Test
void shouldCreateOrderSuccessfully() {
var newOrder = new Order(null, "Laptop", 1500.00);
var savedOrder = new Order(1L, "Laptop", 1500.00);
when(orderRepository.save(any(Order.class)))
.thenReturn(savedOrder); // Mock the repository behavior
var result = orderService.createOrder(newOrder);
assertNotNull(result.getId());
assertEquals("Laptop", result.getItem());
verify(orderRepository).save(newOrder); // Verify interaction
}
}
Common Mistakes & Pitfalls
- Loading Spring unnecessarily: direct construction is enough for plain service logic.
- Testing only mock calls: also assert the service's returned value, state, or exception.
- Verifying implementation noise: check only calls that represent an observable requirement.
- Ignoring error paths: arrange empty results and collaborator failures as well as success.
- Expecting annotations without lifecycle support: initialize
@Mockfields with the Mockito extension or create mocks programmatically.
Check your understanding
Answer these without looking back at the article.
- When should you use @SpringBootTest instead of @ExtendWith(MockitoExtension.class)?
- How do you test a service that has multiple dependencies?
- How do you test a service method that calls two other services?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.