简体   繁体   English

如何在@WebMvcTest中获取Spring Boot默认验证响应?

[英]How to get Spring Boot default validation reponses in @WebMvcTest?

When I run my application and there is a validation problem with the request to one of my endpoints, the response has a lot of information on what exactly went wrong. 当我运行我的应用程序,并且对我的一个端点的请求存在验证问题时,响应中会包含很多有关到底出了什么问题的信息。 It looks similar to this: 它看起来类似于:

{
  "timestamp": "2017-11-23T13:18:05.923+0000",
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.bind.MethodArgumentNotValidException",
  "errors": [
    {
      "codes": [
        "Size.sensorParameters.openText",
        "Size.openText",
        "Size.java.lang.String",
        "Size"
      ],
      "arguments": [
        {
          "codes": [
            "sensorParameters.openText",
            "openText"
          ],
          "arguments": null,
          "defaultMessage": "openText",
          "code": "openText"
        },
        20,
        1
      ],
      "defaultMessage": "size must be between 1 and 20",
      "objectName": "sensorParameters",
      "field": "openText",
      "rejectedValue": "xewrewrwerwer weew ewewewrwe ew wewewewerew we ewrwerwe ewr ",
      "bindingFailure": false,
      "code": "Size"
    }
  ],
  "message": "Validation failed for object='sensorParameters'. Error count: 1",
  "path": "/api/monitors/89c94880-9423-4990-892b-6bbd8e90b1e6/sensors"
}

If I test this in an @WebMvcTest , I do get the same 400 response, but the response body is empty. 如果我在@WebMvcTest对此进行测试, @WebMvcTest得到相同的400响应,但响应主体为空。

Is there some extra setup I should be doing in order to get the same behaviour in my unit test? 为了在单元测试中获得相同的行为,我应该做一些额外的设置吗? I am using the @Autowired MockMvc object I get from the test framework without any extra setup from my side currently. 我正在使用从测试框架获得的@Autowired MockMvc对象,目前我这边没有任何额外的设置。

As Rossen Stoyanchev (the inventor of MockMvc ) pointed out in a related issue on JIRA ... 正如罗森Stoyanchev(发明者MockMvc )在一个相关的问题指出JIRA ...

the body is taken care of by Spring Boot which configures error mappings at the Servlet container level (see here and since Spring MVC Test runs with a mock Servlet request/response, there is no such error mapping. Spring Boot负责在Servlet容器级别配置错误映射(请参见此处,并且由于Spring MVC Test使用模拟的Servlet请求/响应运行,因此没有这样的错误映射)。

He goes on to advise that one use MockMvc to test controller logic and mappings, but that one write an in container integration with Spring Boot to test the error mapping support in the Servlet managed by Spring Boot. 他继续建议人们使用MockMvc来测试控制器逻辑和映射,但是建议人们使用Spring Boot编写容器内集成以测试Spring Boot管理的Servlet中的错误映射支持。

If you feel that Spring Boot should somehow (magically) provide the error mapping support in conjunction with MockMvc , I'd suggest you raise an issue on GitHub against the Spring Boot project. 如果您认为Spring Boot应该以某种方式(神奇地)与MockMvc一起提供错误映射支持,我建议您针对Spring Boot项目在GitHub上提出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM