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:
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:
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
@WebMvcTestwhen the goal is "does the controller behave correctly?" - choose
@SpringBootTestwhen 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
- '
MockMvcis the HTTP testing tool and can be used with both annotations.' - '
@WebMvcTestloads only the web layer and is best for focused controller tests.' - '
@SpringBootTestloads the full application context and is better for integration coverage.' - Add
@AutoConfigureMockMvcwhen you wantMockMvcinside 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.

