0PricingLogin
Spring Boot 4 Microservices & REST APIs · Lesson

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

  1. Unit Testing with JUnit and Mockito
  2. Web Layer Tests with MockMvc
  3. Integration Tests with @SpringBootTest
  4. Real Dependencies with Testcontainers
← Back to Spring Boot 4 Microservices & REST APIs