jsonPath 表达式预期值但 return 值列表

jsonPath expression expected value but return list of values

我想检查响应 "class_type" 值有 "REGION"。

我使用 mockMvc 测试了 springboot API。 MockHttpServletResponse 是这样的。

Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = 
{"result":true,
"code":200,
"desc":"OK",
"data":{"total_count":15567,
        "items": ... 
}}

这是整个响应对象。 让我们仔细看看,尤其是物品。

 "items": [
      {
        "id": ...,
        "class_type": "REGION",
        "region_type": "MULTI_CITY",
        "class": "com.model.Region",
        "code": "AE-65GQ6",
        ...

      },
      {
        "id": "...",
        "class_type": "REGION",
        "region_type": "CITY",
        "class": "com.model.Region",
        "code": "AE-AAN",
        ...

      },

我尝试使用 jsonPath。

@When("User wants to get list of regions, query is {string} page is {int} pageSize is {int}")
    public void userWantsToGetListOfRegionsQueryIsPageIsPageSizeIs(String query, int page, int pageSize) throws Exception {
        mockMvc().perform(get("/api/v1/regions" ))
                .andExpect(status().is2xxSuccessful())
                .andDo(print())
                .andExpect(jsonPath("$.data", is(notNullValue())))
                .andExpect(jsonPath("$.data.total_count").isNumber())
                .andExpect(jsonPath("$.data.items").isArray())
                .andExpect(jsonPath("$.data.items[*].class_type").value("REGION"));

        log.info("지역 목록");
    }

但是

jsonPath("$.data.items[*].class_type").value("REGION")

return

java.lang.AssertionError: Got a list of values ["REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION","REGION"] instead of the expected single value REGION

我只想检查“$.data.items[*].class_type”有 "REGION".

我该如何更改?

一个选项是检查数组中是否有 class_type 等于 'REGION'[= 的元素15=]:

public static final String REGION = "REGION";

mockMvc().perform(get("/api/v1/regions"))
.andExpect(jsonPath("$.data.items[?(@.class_type == '" + REGION + "')]").exists());