In Spring testing, @WebMvcTest is the annotation that lets you test controllers, request mappings and response handling without loading the full application context. It builds a minimal context containing only web-layer beans and configures MockMvc for you, so each test class starts in a fraction of the time a full @SpringBootTest needs.
The trade-off is that nothing outside the web layer exists. That is deliberate: the test proves how HTTP maps to your controller code, while business logic gets its own tests.
Spring Boot offers several test styles, each loading a different amount of the application.
| Test style | Loads | Typical use |
|---|---|---|
| Plain unit test | Nothing from Spring | Services, pure logic |
@WebMvcTest | MVC components only | Controller and HTTP behaviour |
@DataJpaTest | JPA repositories and an embedded DB | Queries and mappings |
@SpringBootTest | The whole application | Integration and end-to-end paths |
Choosing the narrowest slice that proves your point keeps the suite fast, and the test failures point to one layer instead of many.
Starting the full context means creating every service, repository, client and scheduler, often with database connections. A controller test rarely needs any of that. With @WebMvcTest you gain three things:
Spring caches contexts by configuration, so using different mock sets in many classes can create several cached contexts. Keep the set of @MockitoBean declarations similar across classes where you can.
Spring Boot registers a MockMvc bean that exercises the real DispatcherServlet, argument resolvers, converters and exception handlers, but over a mock request instead of a socket.
The ProductService is a Mockito mock managed by the context. If you have used @MockBean, the new annotation is the replacement: @MockBean is deprecated since Spring Boot 3.4 and is gone in Boot 4.
The slice includes Filter beans and, with Spring Security present, the security filter chain. Requests are therefore authenticated unless you say otherwise.
Classes the slice does not scan can be added with @Import. A custom SecurityFilterChain defined in your own configuration class is usually picked up if it is a bean in a scanned configuration, but check that your configuration is imported when tests behave differently from production.
Without Spring Boot, you can build a context-free test with MockMvcBuilders.standaloneSetup(controller), which needs no Spring context at all but skips auto-configuration. A frequent mistake is putting @WebMvcTest on a class together with @SpringBootTest; use one of them. Another is mocking the controller's own helper beans so heavily that the test only checks mocks. Assert on HTTP-visible results, such as status, headers and body.
Spring Framework 6.2 added MockMvcTester, which allows fluent AssertJ assertions on the response and is worth a look in new code.
@WebMvcTest loads only the web layer and mocks the rest; @SpringBootTest loads the full context and is slower but exercises real wiring.
Not with @WebMvcTest, which already configures MockMvc. It is needed when you use @SpringBootTest and want MockMvc.
No. Repositories are not loaded. Use @DataJpaTest or Testcontainers for persistence tests.
JUnit 5 (Jupiter), which Spring Boot uses by default. The old @RunWith(SpringRunner.class) is for JUnit 4 and is not needed.
@WebMvcTest is the focused way to test Spring MVC controllers: it loads only web components, provides MockMvc, and leaves collaborators to @MockitoBean. Use it for mappings, validation, security rules and JSON output, and keep @SpringBootTest for a smaller number of full-stack tests.