Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在@WebMvcTest中获得Spring启动默认验证响应?_Java_Spring_Spring Boot_Spring Test - Fatal编程技术网

Java 如何在@WebMvcTest中获得Spring启动默认验证响应?

Java 如何在@WebMvcTest中获得Spring启动默认验证响应?,java,spring,spring-boot,spring-test,Java,Spring,Spring Boot,Spring Test,当我运行我的应用程序时,对我的一个端点的请求出现验证问题,响应中有很多关于到底出了什么问题的信息。它看起来像这样: { "timestamp": "2017-11-23T13:18:05.923+0000", "status": 400, "error": "Bad Request", "exception": "org.springframework.web.bind.MethodArgumentNotValidException", "errors": [ {

当我运行我的应用程序时,对我的一个端点的请求出现验证问题,响应中有很多关于到底出了什么问题的信息。它看起来像这样:

{
  "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"
}
如果我在
@WebMvcTest
中测试这一点,我确实会得到相同的400响应,但响应主体是空的

为了在单元测试中获得相同的行为,我是否应该进行一些额外的设置?我正在使用从测试框架中获得的
@Autowired
MockMvc对象,目前我没有任何额外的设置。

正如Rossen Stoyanchev(
MockMvc
的发明者)在一篇关于

主体由SpringBoot负责,SpringBoot在Servlet容器级别配置错误映射(请参阅),因为SpringMVC测试使用模拟Servlet请求/响应运行,所以不存在此类错误映射

他继续建议使用
MockMvc
来测试控制器逻辑和映射,但编写一个与springboot的容器内集成来测试springboot管理的Servlet中的错误映射支持

如果您觉得Spring Boot应该以某种方式(神奇地)与
MockMvc
一起提供错误映射支持,那么我建议您针对Spring Boot项目在GitHub上提出一个问题。

正如Rossen Stoyanchev(《代码>MockMvc的发明者》)在一篇关于

主体由SpringBoot负责,SpringBoot在Servlet容器级别配置错误映射(请参阅),因为SpringMVC测试使用模拟Servlet请求/响应运行,所以不存在此类错误映射

他继续建议使用
MockMvc
来测试控制器逻辑和映射,但编写一个与springboot的容器内集成来测试springboot管理的Servlet中的错误映射支持


如果你觉得弹簧靴应该以某种方式(神奇地)结合
MockMvc
提供错误映射支持,我建议您针对Spring Boot项目在GitHub上提出一个问题。

您能为您的测试提供代码吗?首先想到的是@WebMvcTest禁用了完整的自动配置。请参阅doc。是的,可能是这样。但我想知道是否可以添加一些额外的注释来实现这一点异常处理代码在此测试中处于活动状态。您能为您的测试提供代码吗?首先想到的是@WebMvcTest禁用了完全自动配置。请参阅文档。是的,可能是这样。但我想知道是否可以添加一些额外的注释,使异常处理代码在此测试中处于活动状态。