3

i tried to read the content of the json with jsonPath and i get an error.

Here the junit test method:

mockMvc.perform(get("/path")
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id", is(1)))
                .andExpect(jsonPath("$.name", is("NAME")))
                .andReturn().getResponse().getContentAsString();

here is what the request return me:

[{"id":1,"name":"NAME","....}, ....}]

i got this error:

No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes.

can someone helps me.

Thanks

1 Answer 1

5

The response returns a JSON array and using "$.id" you try to access the id property of that array. This - as the error message tells you - is not possible.

Test the id and name property on the first element of the array instead:

.andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$[0].name", is("NAME")))
Sign up to request clarification or add additional context in comments.

Comments

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.