0

I want to check response "class_type" value has "REGION".

I test springboot API using mockMvc. the MockHttpServletResponse is like this.

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": ... 
}}

this is whole response object. Let's take a closer look, especially 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",
        ...

      },

I tried using 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("지역 목록");
    }

but

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

I want to just check "$.data.items[*].class_type" has "REGION".

How can I change this?

horoyoi o
  • 584
  • 1
  • 8
  • 29

1 Answers1

1

One option would be to check whether you have elements in your array which have the class_type equal to 'REGION':

public static final String REGION = "REGION";

mockMvc().perform(get("/api/v1/regions"))
.andExpect(jsonPath("$.data.items[?(@.class_type == '" + REGION + "')]").exists());
Ioan M
  • 1,105
  • 6
  • 16