Reputation: 7417
I am using rest-assured to test a rest api. I have a json response that returns an array of objects. I want verify that it contains objects with specific test of values. Check the following example:
Example JSON:
{
"contents" : [
{
"field1" : "value1",
"field2" : "value2"
},
{
"field1" : "value3",
"field2" : "value4"
}
]
}
How do I write the body assertion so that I can check that regardless of the position I have one entry with:
value1 & value2 in each respective field and another entry with value3 & value4.
...get ("/myEndpoint" )
.then()
.body ( "contents.?????", contains...)
Upvotes: 2
Views: 5001
Reputation: 40510
Depend on what you mean by "regardless of position".
If you mean regardless of position in contents
but it's important that field1
is specified before field2
you can do like this:
when().
get("/myEndpoint").
then().
root("contents.any { it == ['field1':'%s', 'field2':'%s'] }").
body(withArgs("value1", "value2"), is(true)).
body(withArgs("value3", "value4"), is(true));
If you don't care if field1
is specified before field2
you can do like this:
when().
get("/myEndpoint").
then().
root("contents").
body("", hasItems(hasEntry("field2", "value2"), hasEntry("field1", "value1")),
"", hasItems(hasEntry("field1", "value3"), hasEntry("field2", "value4")));
You can read up on "root paths" here.
Upvotes: 1