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?

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry for my ambiguous question. items is under data.
@horoyoio can you please mark the answer accordingly if it solved your issue? Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.