如何断言 json 路径值大于值?
How to assert json path value is greater than value?
给定以下 JSON 从 REST 调用返回:
{"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.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[=12=](JsonPathResultMatchers.java:87)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:196)
at
我还能尝试什么?
JsonPath 运算符[?(<expression>)]
选择与给定表达式匹配的所有元素。因此,结果是一个 json 数组。
在示例中 [?(@.Wert == '')]
匹配所有 json 节点,字段 Wert
具有空值。您的 json 示例只有一个项目与谓词匹配,但通常可能有多个。要解决这个问题,您要么定义一个更具体的表达式,只匹配一个元素,要么调整匹配器以处理集合。
匹配的合集:
.andExpect(jsonPath("$..[?(@.Wert == '')].Anzahl", everyItem(greaterThan(400))))
给定以下 JSON 从 REST 调用返回:
{"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.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[=12=](JsonPathResultMatchers.java:87)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:196)
at
我还能尝试什么?
JsonPath 运算符[?(<expression>)]
选择与给定表达式匹配的所有元素。因此,结果是一个 json 数组。
在示例中 [?(@.Wert == '')]
匹配所有 json 节点,字段 Wert
具有空值。您的 json 示例只有一个项目与谓词匹配,但通常可能有多个。要解决这个问题,您要么定义一个更具体的表达式,只匹配一个元素,要么调整匹配器以处理集合。
匹配的合集:
.andExpect(jsonPath("$..[?(@.Wert == '')].Anzahl", everyItem(greaterThan(400))))