Testing jsonpath that an array contains specfic objects in any order

I am testing a Spring controller that can return 400 field errors. These field errors are an array of objects containing the path and message fields.

Now I want to check that a particular call returns several errors with a specific path and message.

I can not come closer than below:

.andExpect(jsonPath("$.fieldErrors[*].path", containsInAnyOrder("title", "description")))
.andExpect(jsonPath("$.fieldErrors[*].message", containsInAnyOrder(
    "The maximum length of the description is 500 characters.",
    "The maximum length of the title is 100 characters.")));

But this allows you to open the option that bad combinations of "path" and "message" are accepted.

Any ideas on improving jsonpath to check this out?

+4
source share
2 answers

This seems like a better approach:

.andExpect(jsonPath('$.fieldErrors[?(@.path == \'title\' && @.message == \'The maximum length of the title is 100 characters.\')]').exists())
+8
source

, , , . , '& &' jsonpath, (0.8.1), "" 8 , , 2.0:

JSON- 27 -

"" , , , :

.andExpect(jsonPath('$.fieldErrors[?(@.path == \'title\')][?(@.message == \'The maximum length of the title is 100 characters.\')]').exists())
+1

All Articles