MockMvc
Test Spring MVC controllers with MockMvc — the fluent API for testing HTTP request handling, response status, JSON content, and security without starting a real server.
Last reviewed: January 2025
🔍 What Is It?
MockMvc is Spring MVC Test's fluent API for testing controllers.
Key methods:
- →
mockMvc.perform(get("/url"))— execute a request - →
.andExpect(status().isOk())— assert response status - →
.andExpect(jsonPath("$.field").value("expected"))— assert JSON - →
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) - →
.andReturn()— get the MvcResult for further assertions - →
.andDo(print())— print request/response for debugging
🎯 Why Do We Need It?
MockMvc tests the full controller pipeline — argument resolvers, validation, exception handlers, security filters — without starting a real HTTP server. It's faster than RestTemplate/WebTestClient against a running server and gives you precise assertions on the response.
🏗️ What Happens in Real Projects?
Used for:
- →Testing all HTTP endpoints
- →Verifying request validation (400 for invalid input)
- →Testing security (401/403 for unauthorized requests)
- →Testing error responses (404, 409, 500)
- →Testing pagination and filtering
⚙️ How It Works
MockMvc dispatches requests directly to the DispatcherServlet without going through the network stack. The full filter chain, handler mapping, argument resolvers, and message converters all run — just without a real TCP connection.
💻 Example
MockMvc in Practice — Complete controller testing with MockMvc.
🤔 Common Confusions
❓ MockMvc vs WebTestClient — which should I use?
MockMvc is for servlet-based apps (spring-boot-starter-web). WebTestClient is for reactive apps (spring-boot-starter-webflux) but also works with MockMvc via MockMvcWebTestClient. MockMvc has a more mature API for servlet testing. WebTestClient has a cleaner fluent API. For new Spring Boot 3.x projects, either works.
❓ Do I need to start a real server for MockMvc tests?
No. MockMvc dispatches directly to the DispatcherServlet without a network connection. @WebMvcTest never starts a real server. If you use @SpringBootTest(webEnvironment=RANDOM_PORT), a real server starts and you need RestTemplate or WebTestClient to call it.
🚨 Common Production Issues
jsonPath assertions failing due to unexpected JSON structure — use andDo(print()) to inspect the actual response
Security filters not applied in @WebMvcTest — add @Import(SecurityConfig.class) to include security
ObjectMapper in tests using different config than production — use @Autowired ObjectMapper from context
✅ Best Practices
Test both success and error cases for every endpoint
Test authentication and authorization explicitly
Use andDo(print()) during development to inspect request/response
Use jsonPath() for JSON assertions — more readable than string comparison
Test validation by sending invalid requests and asserting 400 + field errors
🏛️ Architect's Note
MockMvc tests are the best ROI in your test suite. They test the full controller pipeline including validation, serialization, and error handling — without the overhead of a real server. Write MockMvc tests for every endpoint: happy path, validation failure, authentication failure, authorization failure, and not-found cases. These tests catch the most common API bugs.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Spring Boot Technology Guide
Return to the Spring Boot learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Spring Boot learning path
Browse the full Spring Boot topic collection and learning order on SplashRide.
Spring Boot Interview Prep
Practice Spring Boot interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Unit Testing
Write effective unit tests for Spring Boot services using JUnit 5, Mockito, and AssertJ — fast, isolated tests that don't start a Spring context.
Integration Testing
Write Spring Boot integration tests with @SpringBootTest, Testcontainers, and @DataJpaTest — testing real component interactions with actual databases.