Discover how to test your web controllers quickly without starting your whole app!
Why @WebMvcTest for controller testing in Spring Boot? - Purpose & Use Cases
Imagine writing tests for your web controllers by starting the whole application and manually sending HTTP requests to check responses.
This approach is slow, requires a full app context, and makes it hard to isolate controller logic from other parts like services or databases.
@WebMvcTest lets you test only the web layer, loading just the controller and related components, so tests run fast and focus on controller behavior.
start full app context send HTTP request check response
@WebMvcTest(MyController.class) mockMvc.perform(get("/path")) .andExpect(status().isOk())
It enables fast, focused tests that verify your controller's behavior without the overhead of starting the entire application.
When adding a new API endpoint, you can quickly write a @WebMvcTest to check if it returns the right status and JSON without waiting for the full app to start.
Manual full app tests are slow and complex.
@WebMvcTest isolates controller testing for speed and focus.
It helps catch web layer issues early and easily.