@WebMvcTest is a Spring Boot test annotation that starts only the Spring MVC slice of your application: controllers, controller advice, filters, converters and security configuration for the web layer. It auto-configures MockMvc so you can send simulated HTTP requests without starting a server, and you replace the services behind the controller with mocks.
That makes controller tests fast and focused on request mapping, validation, serialization and status codes. Services and repositories are not loaded.
@WebMvcTest creates a trimmed application context. It includes beans annotated with @Controller, @RestController, @ControllerAdvice, @JsonComponent, Converter, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver. It does not scan @Service, @Component or @Repository beans, and it does not start an embedded server.
| Annotation | Scope | Speed |
|---|---|---|
@WebMvcTest | Web layer only, MockMvc | Fast |
@DataJpaTest | JPA slice with an in-memory or container database | Fast |
@SpringBootTest | Full application context | Slowest |
In Spring Boot 4 the test annotations were split into per-technology modules, and @WebMvcTest moved to a new package. Let your IDE import the right one for your Boot version; the usage below is the same.
Start with a controller and a service:
The test loads only BookController and a mock BookService:
@MockitoBean replaces @MockBean, which was deprecated in Spring Boot 3.4 and removed in Boot 4. @MockitoBean comes from Spring Framework 6.2 and later. Specifying BookController.class limits the context to one controller, which speeds up the test further.
Request validation is one of the most valuable things to test at this level:
The test proves that @Valid is active and that your @ControllerAdvice maps the failure to the right status, without a database or service.
If Spring Security is on the classpath, @WebMvcTest applies its filter chain, so unauthenticated requests get 401 or 403. Add a request post-processor or the @WithMockUser annotation from spring-security-test:
If your security configuration lives in a custom class, import it with @Import, because only web-layer beans are scanned.
NoSuchBeanDefinitionException for a service: add @MockitoBean for each dependency of the controller.@Configuration class: use @Import to include it..with(csrf()), or the test should use a token.@WebMvcTest to a single controller and avoid adding application-wide @ComponentScan that pulls in everything.@SpringBootTest(webEnvironment = RANDOM_PORT) with RestTestClient or WebTestClient.It sits between the two. The controller and the Spring MVC machinery are real, while collaborators are mocked, so it checks mapping and serialization that plain unit tests cannot.
No. Repositories and data sources are not loaded. Use @DataJpaTest for persistence tests.
Only mocks. Real @Service beans are not part of the slice unless you import them explicitly.
@WebFluxTest with WebTestClient.
@WebMvcTest tests the Spring MVC web layer in isolation: it loads controllers and web infrastructure, provides MockMvc, and expects mocked collaborators through @MockitoBean. Use it for routing, validation, JSON mapping, error handling and security rules, and keep persistence and full-stack checks in other test types.