When I test a service that responds with a 4xx status code (e.g. validation error), then MockHttpServletResponse will contain an empty body instead of valid JSON with the error description.
spring-boot-starter-test: 2.2.5.RELEASE
Sample Project: https://github.com/miron4dev/spring-boot-issue-20345/
In DemoControllerIntegrationTest
there is an assert that a response body should not be an empty string in case of 4xx error. This assert is failing, because the body is empty.
If you execute the same service manually, you'll get next JSON with the error description:
{
"timestamp": "2020-02-28T14:23:53.432+0000",
"status": 400,
"error": "Bad Request",
"errors": [
{
"codes": [
"NotNull.demoRequest.msg",
"NotNull.msg",
"NotNull.java.lang.String",
"NotNull"
],
"arguments": [
{
"codes": [
"demoRequest.msg",
"msg"
],
"arguments": null,
"defaultMessage": "msg",
"code": "msg"
}
],
"defaultMessage": "must not be null",
"objectName": "demoRequest",
"field": "msg",
"rejectedValue": null,
"bindingFailure": false,
"code": "NotNull"
}
],
"message": "Validation failed for object='demoRequest'. Error count: 1",
"path": "/ping"
}
Comment From: wilkinsona
Thanks for the sample. This is a duplicate of #5574. You may want to test your error responses using a running server and TestRestTemplate or TestWebClient. Alternatively, you may want to explore this approach with the caveat that your test is then not exactly testing what will happen at runtime.