Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockMvc doesn't return response content in case of 4xx status codes #20345

Closed
miron4dev opened this issue Feb 28, 2020 · 1 comment
Closed

MockMvc doesn't return response content in case of 4xx status codes #20345

miron4dev opened this issue Feb 28, 2020 · 1 comment
Labels
status: duplicate A duplicate of another issue

Comments

@miron4dev
Copy link

miron4dev commented Feb 28, 2020

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"
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 28, 2020
@wilkinsona
Copy link
Member

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.

@wilkinsona wilkinsona added status: feedback-provided Feedback has been provided status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants