Spring Boot testing
MockMvc
SpringBootTest
WebMvcTest
Java testing

Difference between using MockMvc with SpringBootTest and Using WebMvcTest

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

MockMvc is the HTTP testing tool, while @SpringBootTest and @WebMvcTest decide how much of the application context gets started around it. The real difference is not "MockMvc versus WebMvcTest," but "full application test versus MVC slice test."

MockMvc Is The Client, Not The Scope

MockMvc simulates HTTP requests against Spring MVC without starting a real server socket. You can use it in both styles of test:

  • full application context tests
  • web-layer slice tests

That distinction matters because people often compare MockMvc to @WebMvcTest as if they were alternatives. They are not. MockMvc is the request driver; the annotation controls the test scope.

Use @WebMvcTest For Controller-Focused Tests

@WebMvcTest loads only MVC-related beans such as controllers, controller advice, JSON configuration, and filters relevant to the web slice. It does not load your full service, repository, or messaging stack by default.

Example:

java
1@WebMvcTest(UserController.class)
2class UserControllerWebTest {
3
4    @Autowired
5    private MockMvc mockMvc;
6
7    @MockBean
8    private UserService userService;
9
10    @Test
11    void returnsUserJson() throws Exception {
12        given(userService.findNameById(1L)).willReturn("Ava");
13
14        mockMvc.perform(get("/users/1"))
15            .andExpect(status().isOk())
16            .andExpect(content().json("{\"name\":\"Ava\"}"));
17    }
18}

This is fast and focused. It is ideal when you want to verify request mapping, validation, serialization, and response status without booting the whole application.

Use @SpringBootTest For Full-Context Integration Tests

@SpringBootTest loads the whole application context. If you also want MockMvc, add @AutoConfigureMockMvc:

java
1@SpringBootTest
2@AutoConfigureMockMvc
3class UserControllerIntegrationTest {
4
5    @Autowired
6    private MockMvc mockMvc;
7
8    @Test
9    void endpointWorksInFullContext() throws Exception {
10        mockMvc.perform(get("/actuator/health"))
11            .andExpect(status().isOk());
12    }
13}

This style is slower, but it tests more of the real application wiring:

  • security configuration
  • service beans
  • repositories
  • database integration if configured
  • application properties and startup behavior

Use it when the controller behavior depends on actual collaborating beans and you want confidence in the end-to-end Spring configuration.

Choosing Between Them

A good rule of thumb is:

  • choose @WebMvcTest when the goal is "does the controller behave correctly?"
  • choose @SpringBootTest when the goal is "does the application wiring behave correctly?"

That difference keeps your test suite balanced. Most web endpoints benefit from a small number of full-context tests plus a larger set of slice tests.

If you use only @SpringBootTest, your suite may become slow and harder to isolate. If you use only @WebMvcTest, you may miss wiring bugs outside the controller layer.

What Each Style Does Not Give You

@WebMvcTest does not prove that your real service implementation, repository wiring, or data configuration works.

@SpringBootTest with MockMvc still does not start a real external network server by default. If you specifically want a real port and real HTTP client behavior, use webEnvironment = RANDOM_PORT with a real client such as TestRestTemplate or WebTestClient.

That is an important architectural distinction in Spring tests.

Common Pitfalls

One common mistake is forgetting @AutoConfigureMockMvc when using @SpringBootTest, then wondering why MockMvc is not injected.

Another issue is expecting @WebMvcTest to load service or repository beans automatically. Those dependencies usually need to be mocked.

A third problem is overusing full-context tests for simple controller behavior, which slows the test suite without increasing useful coverage.

Finally, some teams assume a passing @WebMvcTest proves the whole endpoint works in production. It only proves the MVC slice works under the mocked setup.

Summary

  • 'MockMvc is the HTTP testing tool and can be used with both annotations.'
  • '@WebMvcTest loads only the web layer and is best for focused controller tests.'
  • '@SpringBootTest loads the full application context and is better for integration coverage.'
  • Add @AutoConfigureMockMvc when you want MockMvc inside a full-context test.
  • A healthy test suite usually uses both styles for different goals.
  • Pick the annotation based on test scope, not habit or boilerplate.

Course illustration
Course illustration

All Rights Reserved.