If there was an error while processing a request, MockHttpServletResponse.sendError(int status, String errorMessage) only sets this.errorMessage and does not write to this.content. Conversely, MockMvcClientHttpRequestFactory only reads servletResponse.content and ignores the error message.

This results in error response bodies being swallowed in mockMVC tests which use RestTemplate with MockMvcClientHttpRequestFactory.

So, either MockHttpServletResponse.sendError() should write to the outputstream or MockMvcClientHttpRequestFactory should read something like "errorMessage or if null the content byte array".

Comment From: snicoll

Can you share the code that you've used with RestTemplate and MockMvcClientHttpRequestFactory? I don't really see what is actually swallowing the error message. Please share that as a small sample that we can run ourselves.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: gnagy

Hi @snicoll, I created an example project at https://github.com/gnagy/spring-framework-issue-26720 showcasing the issue, with a potential fix.