MockMvc — Testing REST Controllers Without Starting a Server
Fast recall
Quick revision
Core answer
Whether the candidate can use MockMvc effectively for controller testing without starting a full server.
Using @SpringBootTest for controller tests when @WebMvcTest is sufficient and faster.
Demonstrate testing validation, error handling, security, and file upload with MockMvc.
Interview-ready guide
Interview answer
MockMvc simulates HTTP requests through Spring's DispatcherServlet without starting a real server — no TCP, no socket binding. Combined with @WebMvcTest, it tests the controller layer in isolation: request mapping, parameter binding, @Valid annotation processing, @ControllerAdvice exception handling.
The test verifies the full HTTP contract — method, URL, content type, status code, response body, and headers. jsonPath() assertions check specific fields in the JSON response.
The key insight: validation tests don't need service mocks. Spring rejects invalid requests with @Valid before the controller method executes. The test verifies Spring's behavior, not your business logic.
MockMvc's limitation is that it bypasses the transport layer. CORS, SSL, and response compression only work with a real server via @SpringBootTest(webEnvironment = RANDOM_PORT) and TestRestTemplate.
MockMvc — Testing REST Controllers Without Starting a Server
Direct Answer
MockMvc simulates HTTP requests through DispatcherServlet without a real server. Combined with @WebMvcTest, it tests controllers in isolation with millisecond execution.
Common Mistakes & Pitfalls
Using @SpringBootTest for controller tests when @WebMvcTest is sufficient and faster.
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.