To test a Spring MVC controller without a real server, use @WebMvcTest with MockMvc and replace the controller's service with a mock bean. You stub the service, perform a request such as get("/users/1"), and assert the status, headers and JSON body of the response.
This guide targets Spring Boot 3.4 and later (Spring Framework 6.2+), where @MockitoBean replaces the deprecated @MockBean. The other meaning of "mock web responses", faking an external HTTP service your code calls, is covered near the end.
@WebMvcTest starts a slice of the application context: controllers, @ControllerAdvice, filters, converters and the Spring Security configuration for the web layer. It does not load @Service, @Repository or @Component beans, so every dependency of the controller must be provided as a mock. It also auto-configures MockMvc, which dispatches requests through the DispatcherServlet in memory, without opening a port.
| Approach | Loads | Use it for |
|---|---|---|
@WebMvcTest + MockMvc | Web layer only | Routing, validation, JSON, status codes, error handling |
@SpringBootTest + @AutoConfigureMockMvc | Whole application | End-to-end flow with real beans |
MockMvcBuilders.standaloneSetup(...) | One controller, no Spring context | Very fast tests with plain Mockito |
The controller delegates to a service and returns JSON:
The test mocks UserService with @MockitoBean, performs the request and checks the response:
@MockitoBean (package org.springframework.test.context.bean.override.mockito) adds the Mockito mock to the context and resets it after each test. The older @MockBean from Spring Boot still works in Boot 3.x but is deprecated since 3.4 and is not available in Spring Boot 4, so use @MockitoBean in new code. Use @MockitoSpyBean where you used @SpyBean.
A good controller test covers the unhappy paths too. Send a JSON body with content(...) and contentType(...):
The second test passes only if a @ExceptionHandler or @RestControllerAdvice maps UserNotFoundException to a 404 response. Because @WebMvcTest includes @ControllerAdvice classes, that mapping is exercised for real. If you use Spring Security, add @WithMockUser or .with(csrf()) to requests that need authentication or a CSRF token.
Spring Framework 6.2 added MockMvcTester, which wraps MockMvc with AssertJ assertions and is auto-configured for @WebMvcTest in recent Spring Boot versions:
Both styles test the same thing. Pick one per project and stay consistent.
When your service calls another API, mock that server instead of the controller layer:
MockRestServiceServer works with RestTemplate and RestClient and runs in-process: server.expect(requestTo("/api/rates")).andRespond(withSuccess("{\"rate\":1.2}", MediaType.APPLICATION_JSON)).WebClient, HttpClient and contract-style tests, including delays and fault simulation.NoSuchBeanDefinitionException because @WebMvcTest does not scan services.@SpringBootTest for every controller test. It is slower and hides which layer broke.jsonPath on a field with the wrong path, which fails with a path-not-found message. Print the response with .andDo(print()) while debugging.MockMvc runs the request through Spring's DispatcherServlet in the same JVM with no network or servlet container. It is fast and deterministic, but it does not test the embedded server, TLS or real filters outside the Spring context.
Yes. It is deprecated since Spring Boot 3.4 and removed in Spring Boot 4. Use @MockitoBean from Spring Framework 6.2 instead.
Use @WebMvcTest for controller behaviour and @SpringBootTest for a small number of end-to-end tests. The slice test starts in a fraction of the time.
Use multipart("/upload").file(...) for uploads and .header("X-Request-Id", "abc") on the request builder for headers.
Annotate the test with @WebMvcTest(YourController.class), inject MockMvc, replace the service with @MockitoBean, stub it with Mockito, and assert the status, headers and JSON of the response. Cover validation errors and exception mapping as well as the happy path, and use MockRestServiceServer or WireMock when the thing to fake is an outgoing HTTP call.