Spring MockMvc Controller Testing | Java Fresher Interview
Fast recall
Quick revision
Core answer
@WebMvcTest(Controller.class) with MockMvc to test Spring MVC routing, request binding, validation, serialization, controller advice, and configured filters without a live server. Replace a controller collaborator with current @MockitoBean (legacy Boot code uses deprecated @MockBean). If Spring Security is present, its filter chain is normally part of the slice, so authenticate the request or deliberately configure the test boundary.Interview-ready guide
Interview answer
class)withMockMvc` when I need to test the HTTP-to-controller boundary without starting a server.
It can check mappings, query and path binding, bean validation, JSON, controller advice, and configured filters. The service is outside the MVC slice, so on current Spring I replace it with @MockitoBean OrderService; older Boot projects use the now-deprecated @MockBean. perform(get("/orders/7"))` can then assert the status and JSON body.
If Spring Security is configured, its filters normally run too, so the test supplies an authenticated user or JWT and a CSRF token for protected state-changing requests. I would not silently disable filters because that changes the boundary being verified. A live-server test is more suitable when the real network/server stack is part of the requirement.
Spring MockMvc Controller Testing | Java Fresher Interview
Direct Answer
Use @WebMvcTest(Controller.class) with MockMvc to test Spring MVC routing, request binding, validation, serialization, controller advice, and configured filters without a live server. Replace a controller collaborator with current @MockitoBean (legacy Boot code uses deprecated @MockBean). If Spring Security is present, its filter chain is normally part of the slice, so authenticate the request or deliberately configure the test boundary.
Code Example
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired MockMvc mvc;
@MockitoBean OrderService service;
@Test
@WithMockUser
void returnsAnOrder() throws Exception {
when(service.find(7L)).thenReturn(new OrderResponse(7L, "READY"));
mvc.perform(get("/orders/{id}", 7L))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(7))
.andExpect(jsonPath("$.status").value("READY"));
}
}
Common Mistakes & Pitfalls
- Calling it a plain unit test: a
@WebMvcTeststill creates a Spring test context. - Forgetting collaborators: services outside the MVC slice must be imported or overridden.
- Ignoring security: a request can return 401 or 403 before reaching the controller.
- Forgetting CSRF: state-changing requests with CSRF protection enabled need a valid test token.
- Overstating standalone setup: it does not reproduce every MVC component unless configured.
Check your understanding
Answer these without looking back at the article.
- How do you test a POST endpoint with request body using MockMvc?
- How do you test exception handling with @ControllerAdvice?
- What is the difference between @WebMvcTest and @SpringBootTest?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.