@WebMvcTest is a Spring Boot test-slice annotation that starts only the Spring MVC part of your application: controllers, @ControllerAdvice, JSON converters, filters and a MockMvc instance. Services and repositories are not loaded, so you mock them. The result is a fast test of request mapping, validation, serialisation and status codes without a server or database.
Use it when the question is "does this endpoint behave correctly over HTTP?" and use @SpringBootTest when you need the whole application wired together.
The annotation applies the Spring Boot auto-configuration for the MVC layer only. It scans for beans of these kinds:
@Controller and @RestController classes@ControllerAdvice and @JsonComponentConverter, GenericConverter, Filter, HandlerInterceptor and WebMvcConfigurer beansObjectMapper and Spring Security configuration when presentRegular @Component, @Service and @Repository beans are not scanned. If a controller depends on one, the context fails to start with a missing bean error unless you provide a mock. Passing a class, as in @WebMvcTest(UserController.class), limits the test to that one controller.
Here is a controller and a test for it, written for Spring Boot 3.4 or later.
MockMvc calls the DispatcherServlet directly, so no port is opened. The test only checks the web layer: the mapping, path variable conversion, the response status and the JSON body. The service logic is out of scope, which is why it is mocked.
Older tutorials use @RunWith(SpringRunner.class) and @MockBean. Both are outdated. JUnit 5 is the default, so the runner annotation is not needed, and @MockBean was deprecated in Spring Boot 3.4 in favour of Spring Framework's @MockitoBean. Boot 4 removes the old annotation, so migrate when you upgrade.
| Old style | Current style |
|---|---|
@RunWith(SpringRunner.class) (JUnit 4) | not needed with JUnit 5 |
@MockBean | @MockitoBean |
@SpyBean | @MockitoSpyBean |
In Spring Boot 4 the @WebMvcTest annotation lives in a new package under org.springframework.boot.webmvc.test.autoconfigure, and it comes from the spring-boot-starter-webmvc-test dependency. Check the release notes of the version you use for the exact imports.
Combine ObjectMapper with MockMvc to send JSON and check validation behaviour.
Because @ControllerAdvice classes are included in the slice, your global exception handling is tested too, which makes this a good place to confirm error response formats.
If Spring Security is on the classpath, the slice applies it, and unauthenticated requests get 401 or a redirect. Add spring-security-test and use @WithMockUser or SecurityMockMvcRequestPostProcessors.csrf() for POST requests. Use @Import(MyConfig.class) to bring in a configuration class the slice would otherwise skip, and @TestPropertySource or properties to override settings.
No. It uses a mock servlet environment. Use @SpringBootTest(webEnvironment = RANDOM_PORT) if you need a real server.
A controller depends on a bean the slice does not load. Add @MockitoBean for it or @Import the configuration.
No. Use @WebFluxTest for reactive controllers.
Spring Framework 6.2 added MockMvcTester, which offers AssertJ-style assertions as an alternative to jsonPath matchers.
@WebMvcTest tests the web layer in isolation: it loads controllers, advice, converters and filters, supplies MockMvc, and expects you to mock the rest. Prefer @MockitoBean over the deprecated @MockBean, drop the JUnit 4 runner, and reach for @SpringBootTest only when you need the full context.