Quick revision
The short answer
MockMvc runs the DispatcherServlet with mock request and response objects inside the test, not inside a real servlet container. So container behaviour never happens: errors are not forwarded to the /error page, connector settings such as compression, maximum header size and character encoding are not applied, and async or streaming responses need an explicit asyncDispatch. When a bug lives in that layer, MockMvc passes while the real server fails, so we add a few RANDOM_PORT tests for container-level behaviour.
What to remember
In this answer, we saw that MockMvc runs the DispatcherServlet with mock objects instead of a servlet container. Error pages are not forwarded to /error, connector settings such as compression, header size and character encoding are not applied, and async or streaming responses need asyncDispatch. MockMvc stays the main controller test tool, and a few RANDOM_PORT tests cover error responses, encoding, headers and streaming.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
MockMvc runs the DispatcherServlet with mock request and response objects inside the test, not inside a real servlet container. So container behaviour never happens: errors are not forwarded to the /error page, connector settings such as compression, maximum header size and character encoding are not applied, and async or streaming responses need an explicit asyncDispatch. When a bug lives in that layer, MockMvc passes while the real server fails, so we add a few RANDOM_PORT tests for container-level behaviour.
Why this question is asked
A green MockMvc test followed by a 500 in production is confusing until we see where the test stops. MockMvc is excellent for the controller layer, but it is not a server, and some production behaviour belongs to the server. Interviewers ask this to see whether we know the boundary of our tools and can choose the right test for a bug. In this answer, we will recap how MockMvc runs a request, reproduce an error page difference and an encoding difference, look at async and connector settings, and decide which tests to add.
1A Quick Recap of How MockMvc Runs a Request
In production, a request arrives at the servlet container, for example Tomcat. The container parses the HTTP bytes, applies connector settings, runs registered filters, calls Spring's DispatcherServlet, and turns the response object back into bytes. If something goes wrong, the container also handles error pages.
MockMvc skips the container. It builds a MockHttpServletRequest in memory, runs the configured filters and the DispatcherServlet directly on the test thread, and gives us the MockHttpServletResponse to inspect. No socket is opened and no bytes are written. That is why it is fast, and it is also exactly why some failures are invisible to it.
| Layer | MockMvc | Real server (RANDOM_PORT) |
|---|---|---|
| Controllers, validation, advice | Yes | Yes |
| Filters registered as beans | Yes | Yes |
| /error forwarding and error pages | No | Yes |
| Compression, header size limits | No | Yes |
| Charset as written on the wire | Partly, mock response | Yes |
| Async and streaming | Needs asyncDispatch | Yes |
2Difference 1: Error Pages Are Not Forwarded
Suppose a controller calls response.sendError(500), or an exception escapes that no @ControllerAdvice handles. In a real server, the container records the error and forwards the request to /error, where Spring Boot's BasicErrorController renders a JSON error body. If that forward fails, for example because a filter rejects the forwarded request or a custom error view throws, the client gets a broken 500.
Notice that MockMvc either rethrows the exception into the test or leaves an empty 500; it never runs the error path. So a bug in that path, say a security filter that blocks /error and turns every failure into a 403, or an error view that fails to render, can only be seen with a real server.
3Difference 2: Encoding and Connector Settings
A MockHttpServletResponse is an object in memory. Character encoding, compression and header limits are applied by the container's connector when bytes are written or read, so in MockMvc they mostly do not exist.
The same goes for server.compression settings, which only the real connector applies, and server.max-http-request-header-size: a request with a large cookie or token header can pass in MockMvc and be rejected by Tomcat with 400 in production. These are configuration bugs, so only a test that goes through the real connector can find them.
4Difference 3: Async and Streaming Responses
When a controller returns a CompletableFuture, a DeferredResult or a StreamingResponseBody, Spring starts servlet async processing: the request thread is released and the response is completed later. In a real server the container drives this. In MockMvc, the first perform() only starts the async work; we must dispatch again to get the final result.
Without asyncDispatch, a test can assert on a response that is not finished yet. And timeouts, response buffer flushing, and an exception thrown after the first bytes were already sent, which leaves the client with a truncated response instead of a 500, only behave realistically in a real container.
5What to Add: A Few Real-Server Tests
The answer is not to abandon MockMvc. It stays the main tool for controller behaviour. We add a small set of @SpringBootTest(webEnvironment = RANDOM_PORT) tests that target the container layer: one for the error response shape, one for character encoding of a response with non-ASCII text, one for a large header if the service receives tokens, and the async or streaming endpoints.
When a production bug slips through, we reproduce it first in a test of this kind, see it fail, fix it, and keep the test. That turns one incident into lasting coverage of the layer MockMvc cannot see.
6Follow-up questions
Can MockMvc be configured to forward errors to /error?
Not the way a container does. Some teams call the error endpoint directly in a separate test to check its output, but the forward itself, and anything that interferes with it, is only exercised by a real server.
Why does MockMvc throw the exception instead of returning 500?
Because there is no container to catch it. The exception reaches the DispatcherServlet, no handler resolves it, and it propagates out of perform() wrapped in a ServletException. That is useful in tests, since we see the real cause, but it is not what clients experience.
Would WebTestClient bound to MockMvc fix this?
No. Binding WebTestClient to MockMvc only changes the assertion API; requests still go through mock objects. To test the container we bind the client to a real server on a random port.
7Conclusion
In this answer, we saw that MockMvc runs the DispatcherServlet with mock objects instead of a servlet container. Error pages are not forwarded to /error, connector settings such as compression, header size and character encoding are not applied, and async or streaming responses need asyncDispatch. MockMvc stays the main controller test tool, and a few RANDOM_PORT tests cover error responses, encoding, headers and streaming.
Open with
MockMvc is not a server. It builds a mock request, runs the filters and the DispatcherServlet on the test thread, and gives me a mock response object, so anything the servlet container does is skipped.
Then, if they want more
The first big difference is error handling: in a real server, an unhandled exception or sendError makes the container forward to /error, where Boot renders the error body. MockMvc never does that forward; it either throws the exception out of perform or leaves an empty 500.
So a bug in the error path, like a filter blocking /error or an error view that fails, only appears in production.
The second difference is connector settings: character encoding as written on the wire, compression and maximum header size are applied by Tomcat, so a response with non-ASCII text or a request with a huge token header can pass in MockMvc and fail for real.
The third is async and streaming: MockMvc needs asyncDispatch to complete those, and late failures behave differently. My fix is to keep MockMvc for controller logic and add a small set of RANDOM_PORT tests for the container layer, like the error body, encoding and large headers, and to reproduce this production bug in one of them before fixing it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference, Testing
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A MockMvc test passes but the same request fails in production with a 500 because of a...": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/41f40a459a/