Web Layer Tests with MockMvc
Test controllers with @WebMvcTest.
Testing the Web Layer
Web layer tests verify your controllers — request mapping, validation, serialization, and status codes — without starting a full server.
Spring Boot provides @WebMvcTest and MockMvc for this.
@WebMvcTest
@WebMvcTest loads only the web slice: controllers, filters, and converters. Services and repositories are not loaded — you mock them.
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
MockMvc mockMvc;
@MockBean
UserService userService;
}All lessons in this course
- Unit Testing with JUnit and Mockito
- Web Layer Tests with MockMvc
- Integration Tests with @SpringBootTest
- Real Dependencies with Testcontainers