bad*_*son 1 integration-testing jsonpath spring-boot mockmvc
我正在做集成测试,返回值是一个json对象数组。下面这行代码匹配没有错误:
mockMvc.perform(MockMvcRequestBuilders.get(uri)
.param("name", text)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].name", Matchers.containsString("div3")));
Run Code Online (Sandbox Code Playgroud)
但是当我更改$[0].name为时$[*].name出现错误
java.lang.AssertionError: JSON path "$[*].name"
Expected: a string containing "div3"
but: was a net.minidev.json.JSONArray (<["Testing Custom Searches div3: 1","Testing Custom Searches div3: 4","Testing Custom Searches div3: 7","Testing Custom Searches div3: 10","Testing Custom Searches div3: 13","Testing Custom Searches div3: 16","Testing Custom Searches div3: 19"]>)
Run Code Online (Sandbox Code Playgroud)
我一直在四处寻找,但没有找到答案...有没有办法检查每个 *.name 元素是否包含给定的子字符串?
这是因为Matchers.containsString()期望一个String输入但$[*].name返回net.minidev.json.JSONArray一个ArrayList。
您可以使用Matchers.everyItem与列表匹配:
MockMvcResultMatchers.jsonPath("$[*].name", Matchers.everyItem(Matchers.containsString("div3")))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2664 次 |
| 最近记录: |