作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定以下从 REST 调用返回的 JSON:
{"Statistik Eintraege":
[{"Wert":"1","Anzahl":41},
{"Wert":"","Anzahl":482},
{"Wert":"-3","Anzahl":1},
{"Wert":"-1","Anzahl":3},
{"Wert":"-2","Anzahl":3}],
"Statistik Typ":"BlahStatistik"}
...我想验证 Wert='' 的 'Anzahl' 是否大于 400(在本例中为:482)。
我在 java 集成测试中尝试的是:
.andExpect(jsonPath("$..[?(@.Wert == '')].Anzahl", greaterThan(400)));
The exception:
java.lang.ClassCastException: class net.minidev.json.JSONArray cannot be cast to class java.lang.Comparable (net.minidev.json.JSONArray is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
at org.hamcrest.comparator.ComparatorMatcherBuilder$1.compare(ComparatorMatcherBuilder.java:22)
at org.hamcrest.comparator.ComparatorMatcherBuilder$ComparatorMatcher.describeMismatchSafely(ComparatorMatcherBuilder.java:86)
at org.hamcrest.TypeSafeMatcher.describeMismatch(TypeSafeMatcher.java:82)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers.lambda$value$0(JsonPathResultMatchers.java:87)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
at
我还能尝试什么?
最佳答案
JsonPath 运算符 [?(<expression>)]
选择与给定表达式匹配的所有元素。因此,结果是一个json数组。
在例子中[?(@.Wert == '')]
将所有 json 节点与字段 Wert
匹配有一个空值。您的 json 示例只有与谓词匹配的单个项目,但通常可能有多个。要解决这个问题,您要么定义一个更具体的表达式,只匹配一个元素,要么调整匹配器以处理集合。
匹配集合:
.andExpect(jsonPath("$..[?(@.Wert == '')].Anzahl", everyItem(greaterThan(400))))
关于json - 如何断言 json 路径值大于值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68410880/
我是一名优秀的程序员,十分优秀!