What @WebMvcTest Excludes
Fast recall
Quick revision
Core answer
@MockitoBean (or older @MockBean) or import a small real configuration when that is what the test needs.Understanding of what the slice actually excludes and how to handle missing dependencies in controller tests
Assuming every application bean is discovered by the MVC slice or that every missing collaborator must be mocked
Mention that filters and security auto-configuration may participate when Spring Security is present, and that the test should model authentication rather than disable security by habit
Interview-ready guide
Interview answer
@WebMvcTest limits discovery to MVC-related types, so ordinary service and repository beans are normally absent. It can still include controller advice, converters, filters, security configuration types, imports, and explicit test beans. If a controller collaborator is missing, I provide a Mockito bean override with @MockitoBean, use @MockBean on older Boot, or import a small real configuration when that behavior belongs in the test. With Spring Security present, filter-chain behavior may affect MockMvc requests, so I model the relevant user and authorization scenario instead of disabling security automatically.
What @WebMvcTest Excludes
Direct Answer
@WebMvcTest limits component scanning to MVC-related types and does not discover ordinary service or repository beans. It does include supported web infrastructure such as controllers, advice, converters, filters, and security configuration types. Supply a controller dependency with @MockitoBean (or older @MockBean) or import a small real configuration when that is what the test needs.
Code Example
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService; // Service not loaded, must mock
@MockBean
private EmailNotificationService emailService; // Another dependency
@Test
void shouldCreateUserSuccessfully() throws Exception {
var newUser = new User(null, "alice@example.com");
var savedUser = new User(10L, "alice@example.com");
when(userService.createUser(any(User.class))).thenReturn(savedUser);
mockMvc.perform(post("/api/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"email\":\"alice@example.com\"}"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(10));
}
}
Common Mistakes & Pitfalls
- Treating the slice as an exact bean whitelist: Imports and supported web configuration types can add beans beyond the selected controller.
- Using a fixed dependency-count rule: The design problem is unclear responsibilities, not a universal maximum number.
- Disabling security to make the test green: If security is part of the public behavior, model an authenticated or unauthenticated request and assert the expected result.
Check your understanding
Answer these without looking back at the article.
- How do you handle Spring Security in @WebMvcTest?
- What is @WithMockUser and when do you need it?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.