Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring 测试jsonpath数组是否以任何顺序包含特定对象_Spring_Spring Mvc_Spring Test_Jsonpath - Fatal编程技术网

Spring 测试jsonpath数组是否以任何顺序包含特定对象

Spring 测试jsonpath数组是否以任何顺序包含特定对象,spring,spring-mvc,spring-test,jsonpath,Spring,Spring Mvc,Spring Test,Jsonpath,我正在测试一个Spring控制器,它可以返回一个有字段错误的400。这些字段错误是包含“路径”和“消息”字段的对象数组 现在我想测试某个特定的调用是否会返回多个带有特定路径和消息的错误 我无法接近下面的任何内容: .andExpect(jsonPath("$.fieldErrors[*].path", containsInAnyOrder("title", "description"))) .andExpect(jsonPath("$.fieldErrors[*].message", conta

我正在测试一个Spring控制器,它可以返回一个有字段错误的400。这些字段错误是包含“路径”和“消息”字段的对象数组

现在我想测试某个特定的调用是否会返回多个带有特定路径和消息的错误

我无法接近下面的任何内容:

.andExpect(jsonPath("$.fieldErrors[*].path", containsInAnyOrder("title", "description")))
.andExpect(jsonPath("$.fieldErrors[*].message", containsInAnyOrder(
    "The maximum length of the description is 500 characters.",
    "The maximum length of the title is 100 characters.")));
但这使选项保持开放,即接受“路径”和“消息”的错误组合


如何改进jsonpath来测试这一点?这似乎是更好的方法:

.andExpect(jsonPath('$.fieldErrors[?(@.path == \'title\' && @.message == \'The maximum length of the title is 100 characters.\')]').exists())

只是扩展了Marcel提供的答案,因为它帮助我解决了同样的问题,但我不得不稍微修改一下。我发现我们目前在一个项目(0.8.1)中使用的jsonpath版本不支持“&&”运算符,根据这个问题,它在12月8日被标记为“完成”,这意味着它应该在最新版本2.0中可用:

我最后使用的逻辑AND的“旧”语法也显示在该问题上,使用上面的示例将是:

.andExpect(jsonPath('$.fieldErrors[?(@.path == \'title\')][?(@.message == \'The maximum length of the title is 100 characters.\')]').exists())

您可以使用
org.hamcrest.Matchers.containsInAnyOrder()

.andExpect(jsonPath("$.fieldErrors[*].path", Matchers.contains("str1", "str2")))