👉No Tomcat, No real network...
When testing Spring MVC applications, you have probably used:
▪️ @AutoConfigureMockMvc
▪️ MockMvc
▪️ MockMvcTester
But what exactly is being mocked?
By default:
@SpringBootTest
is equivalent to:
@SpringBootTest(webEnvironment = MOCK)
This means:
▪️ No Tomcat server is started
▪️ No real network request is sent
▪️ Spring Boot loads a mock Servlet environment
When you call:
mockMvc.perform(...)
or:
mockMvcTester.get()
.uri("/users")
.exchange();
the testing client creates a mock HTTP request and calls Spring’s DispatcherServlet directly.
The request still reaches your filters, controllers and Spring MVC infrastructure; but entirely inside the test process.
🔸 WHY DOES THIS MATTER?
▪️ Container features are unavailable
Tomcat-specific features such as custom Valves, real session handling or error dispatch cannot be fully tested with MockMvc.
▪️ The controller runs in the test thread
Your test, DispatcherServlet and controller usually execute in the same thread.
This is particularly useful for thread-local data and Spring Security testing utilities.
▪️ MockMvc is only for Spring MVC
It cannot test a WebFlux-only application. For reactive applications, use WebTestClient.
📘 This post is inspired by chapter 4 of Daniel Garnier-Moiroux’s upcoming Manning book, Testing Spring Boot Applications, available through the MEAP program.👉https://hubs.la/Q0473JSS0
#Spring #SpringBoot #Testing #java #book #manning
Go further with Java certification:
Java👇
Spring👇
SpringBook👇
JavaBook👇