MockMvc is the main entry point of Spring MVC Test. It lets you send simulated HTTP requests to your controllers and assert on the status, headers, view and body of the response, all without starting a servlet container or opening a network port.
It runs the real DispatcherServlet and MVC infrastructure (request mapping, validation, message converters, exception handlers), so it tests controller behaviour much more realistically than calling controller methods directly, and much faster than a full HTTP server test.
MockMvc is a class in org.springframework.test.web.servlet. You call perform(RequestBuilder) with a request, and it returns a ResultActions object that you chain with andExpect(...), andDo(...) and andReturn().
What runs in a MockMvc test:
@Valid arguments.@ControllerAdvice exception handling, interceptors and filters you register.What does not run: a real Tomcat or Netty server, real sockets, or HTTP-level behaviour such as chunked encoding.
For a web-layer test, use @WebMvcTest. It loads only controllers, @ControllerAdvice, converters and filters, and injects MockMvc:
If you use @SpringBootTest and still want MockMvc, add @AutoConfigureMockMvc to the class. @MockitoBean (Spring Framework 6.2 and later) replaces the deprecated @MockBean.
The text block keeps the JSON readable. For production code, serialise a DTO with an injected ObjectMapper (or JsonMapper in Jackson 3 setups) instead of building strings by hand.
An invalid body should produce a 400 response:
With Spring Security, use @WithMockUser and include csrf() for state-changing requests:
Add .andDo(print()) (static import from MockMvcResultHandlers) to write the request, handler, model and response to the console. It is the fastest way to see why a mapping returned 404 or a converter failed. Remove it once the test passes, or use log() to send the output to the logger.
| Tool | Starts a server | Best for |
|---|---|---|
MockMvc | No | Servlet-stack controller tests, fast |
MockMvcTester (Spring 6.2+) | No | Same as MockMvc with AssertJ assertions |
WebTestClient | Optional | Reactive apps, or real server tests |
TestRestTemplate / RestTestClient | Yes | Full end-to-end HTTP tests |
MockMvcTester wraps MockMvc with fluent AssertJ assertions. For reactive WebFlux controllers, use WebTestClient, not MockMvc.
It sits between the two. With @WebMvcTest it tests the web layer in isolation with mocked services, which many teams call a slice test.
No. It targets Spring MVC on the servlet stack. Use WebTestClient for WebFlux.
Use jsonPath("$.field") matchers or content().json(expected). Both need the JSON Path library, which spring-boot-starter-test includes.
Typically the controller is not part of the test context (check @WebMvcTest(Controller.class)), or the path or context path is wrong. Use andDo(print()) to inspect it.
MockMvc tests Spring MVC controllers by simulating requests inside the JVM. Use @WebMvcTest with @MockitoBean for focused web-layer tests, build requests with MockMvcRequestBuilders, assert with MockMvcResultMatchers, and use a real-server test only when you need the full HTTP stack.