@WebMvcTest Purpose
Fast recall
Quick revision
Core answer
Whether the candidate understands slice testing and can isolate the web layer for focused controller tests
Using @SpringBootTest for every test, loading the entire application context and making tests slow
Explain that missing controller collaborators can be supplied with current @MockitoBean, older Boot @MockBean, @Import, or a small test configuration, depending on whether the collaborator should be mocked or real
Interview-ready guide
Interview answer
@WebMvcTest is Spring Boot's focused MVC slice. It selects a controller-oriented context, applies MVC test auto-configuration, and gives me MockMvc without starting a real server. Ordinary service and repository beans are not discovered, so I provide a required collaborator with @MockitoBean in current Spring, @MockBean on older Boot, or @Import when I intentionally want a small real component. I use the slice for request mappings, validation, serialization, controller advice, filters, and security behavior at the MVC boundary. It normally has less setup than @SpringBootTest, but the time is configuration-dependent and a failing test is not automatically proof of a controller-code bug.
@WebMvcTest Purpose
Direct Answer
@WebMvcTest is a Spring Boot MVC test slice. It limits scanning to controllers and other supported MVC-related types, applies MVC-focused auto-configuration, and auto-configures MockMvc without opening a server port. Ordinary service and repository beans are not discovered, but filters, controller advice, converters, security configuration types, imports, and test bean overrides can all be part of the context.
Code Example
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private OrderService orderService; // Service not loaded, must mock it
@Test
void shouldReturnOrderById() throws Exception {
var order = new Order(1L, "Laptop", 999.99);
when(orderService.findById(1L)).thenReturn(Optional.of(order));
mockMvc.perform(get("/api/orders/1")) // Simulate GET request
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.item").value("Laptop"));
}
}
Common Mistakes & Pitfalls
- Leaving a required collaborator absent: Supply it as @MockitoBean, older @MockBean, or imported real configuration according to the test boundary.
- Assuming fixed performance: The slice usually loads less than a broad context, but imports, filters, security, and custom configuration still affect startup.
- Assuming every mock must be stubbed: Stub only calls whose default Mockito result does not express the scenario. The failure from an unstubbed call depends on the controller behavior.
Check your understanding
Answer these without looking back at the article.
- How do you inject MockMvc in a @WebMvcTest class?
- What is @MockBean and how does it differ from @Mock?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.