MockMvc is the Spring TestContext class that lets you test Spring MVC controllers by sending mock HTTP requests through the real DispatcherServlet in memory. It matters because it checks the web contract (URL mapping, parameter binding, validation, JSON, status codes, security filters) quickly and without opening a port or deploying to a container.
You can use it in plain Spring tests as well as in Spring Boot, either built from the WebApplicationContext or injected through @WebMvcTest.
MockMvc takes a MockHttpServletRequest, runs it through registered filters and the DispatcherServlet, and returns a MockHttpServletResponse wrapped in a ResultActions object. Handler mappings, argument resolvers, HttpMessageConverter instances and @ControllerAdvice all behave as they do in production. No servlet container, socket or network is used, so tests run in milliseconds.
In a plain Spring project you build it from the web application context.
For a fast test of one controller with no context, use standalone setup:
Standalone mode does not load configuration, so you must register advice, converters or validators yourself.
@WebMvcTest registers MockMvc automatically. For a full application context, add @AutoConfigureMockMvc to a @SpringBootTest class. @MockitoBean replaced the deprecated @MockBean (deprecated in Boot 3.4, removed in 4.0).
| Need | API |
|---|---|
| Request parameters, headers, cookies | .param(), .header(), .cookie() |
| Path variables | get("/users/{id}", 5) |
| File upload | multipart("/files").file(...) |
| Status, headers, body | status(), header(), content(), jsonPath() |
| View and model (server-side rendering) | view(), model(), forwardedUrl(), redirectedUrl() |
| Debugging | .andDo(print()) |
Add spring-security-test to apply authentication to requests: with(user("ann").roles("ADMIN")), with(jwt()) and with(csrf()). Because filters run, a missing token returns 401 or 403 exactly as in production. Exception handlers are covered too: throw an exception from the mocked service and assert the status and error body that your @ControllerAdvice produces.
WebTestClient is the right tool.asyncDispatch on the MvcResult.Spring Framework 6.2 also adds MockMvcTester, a wrapper with AssertJ-style assertions.
It sits between the two. It integrates controller, mapping and conversion layers, but replaces the servlet container and usually the service layer.
Use jsonPath("$.field") or content().json("{...}") with the JSONassert library included in Spring Boot's test starter.
A builder that configures MockMvc for specified controllers without loading a Spring context. It is quick but skips your configuration.
No. For real HTTP calls use TestRestTemplate, RestTestClient or WebTestClient with a started server.
MockMvc runs requests through Spring MVC in memory so you can verify mappings, binding, validation, JSON, security and error handling without a server. Use @WebMvcTest in Boot, webAppContextSetup or standaloneSetup in plain Spring, and add end-to-end tests with a real server for container-level behaviour.