MockMvc is a Spring test class that sends simulated HTTP requests to your Spring MVC controllers and lets you assert on the response, without starting a real server. Its significance is speed and precision: you exercise request mapping, validation, JSON conversion, exception handling and security filters in milliseconds, in the same JVM as the test.
In Spring Boot you normally get a MockMvc instance from @WebMvcTest or @AutoConfigureMockMvc.
MockMvc drives the DispatcherServlet directly with mock request and response objects. Everything in the MVC pipeline runs: handler mapping, argument resolvers, @Valid validation, message converters, @ControllerAdvice and servlet filters. What does not run is a real servlet container, a network socket or an embedded server port. That makes it faster than @SpringBootTest(webEnvironment = RANDOM_PORT) with a real HTTP client, but it also means container-specific behaviour (TLS, real compression, container error pages) is not covered.
For a controller slice, use @WebMvcTest, which loads only web-layer beans.
The service is mocked because @WebMvcTest does not load @Service beans. On Spring Boot 3.4 and later use @MockitoBean; the older @MockBean is deprecated and was removed in Boot 4.0. To test against the full application context, add @SpringBootTest with @AutoConfigureMockMvc.
Commonly used pieces:
MockMvcRequestBuilders: get, post, put, delete, multipart, with .param, .header, .cookie and .content.MockMvcResultMatchers: status(), content(), header(), jsonPath(), view(), model(), redirectedUrl().andDo(print()) to dump the request and response while debugging.andReturn() to get the MvcResult for custom assertions.With Spring Security on the classpath, add spring-security-test and use its helpers.
@WithMockUser also works at method level. Remember csrf() for state-changing requests when CSRF protection is on.
Spring Framework 6.2 added MockMvcTester, which wraps MockMvc and returns results that use AssertJ assertions instead of ResultMatcher chains.
Spring Boot registers a MockMvcTester bean next to MockMvc in MVC tests. Both are valid; pick one style per project.
| Tool | Real server | Best for |
|---|---|---|
MockMvc | No | Controller behaviour, validation, security rules |
@SpringBootTest with TestRestTemplate or WebTestClient | Yes | End-to-end behaviour over HTTP |
MockMvcTester | No | Same as MockMvc with AssertJ |
| Plain unit test of controller | No | Logic only, no mapping |
No. It calls the DispatcherServlet in memory with mock request objects, so no port is opened.
@WebMvcTest is a slice that loads only web beans and configures MockMvc. @AutoConfigureMockMvc adds MockMvc to a larger test such as @SpringBootTest.
No. Use WebTestClient for WebFlux applications.
Usually the controller is not in the slice (wrong class in @WebMvcTest) or the path or context path differs. Print the result with andDo(print()).
MockMvc tests Spring MVC controllers through the real DispatcherServlet without a server, making web-layer tests fast and deterministic. Combine it with @WebMvcTest and @MockitoBean, use Spring Security test helpers for secured routes, and consider MockMvcTester for AssertJ-style assertions.