I'm using RestAssured and Hamcrest to code some integration tests. All was fine until I tried to check nested property.
My Json body is :
{ "rows": [ { "uid": "927e2362-babb-47cc-8406-d618b0e15b89", "owner": "myself" }, { "uid": "6d39c473-d0bd-496e-be86-40917aa3af79", "owner": "myself" } ] }
My test code is like that :
ValidatableResponse response = request.when().get(path).then(); response.statusCode(HttpStatus.SC_OK);
Now I'm trying to add a check that all rows have the property owner
with the value myself
Best I could come up is :
response.body("rows.owner", everyItem(is("myself")));
However I would much prefer do it using the matcherMatchers.hasProperty("owner")
or evenHasPropertyWithValue.hasProperty("owner", is("myself")
Since for some other test, the value of the owner property is random and I just want to check the property is there and not null.
I've tried several ways with no luck :
List<Object> o = response.extract().jsonPath().get("rows"); assertThat(o, hasItem(hasProperty("owner"))); assertThat(o.get(0),Matchers.hasProperty(TestConstants.DOC_OWNER));
But I get everytime
Expected: a collection containing hasProperty("owner") but: no "owner" in <{owner=myself, uid=927e2362-babb-47cc-8406-d618b0e15b89>, no "owner" in <{owner=myself, uid=6d39c473-d0bd-496e-be86-40917aa3af79>
I've seen this subjet which explains that I may end up with an array of array.
But I cannot see how to do it the simpliest way :
- Check that every item of my list has property "owner" and that its value is always "myself"
Thank you for your help
--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
List<String> owners = jsonPath.get("rows.owner");
// Check that every row has an owner
assertFalse(owners.stream().anyMatch(owner -> owner == null));
// Check that every owner is myself
assertFalse(owners.stream().anyMatch(owner -> !owner.equals("myself")));
--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
OK, well if you don't find a Hamcrest approach you like, you could always just do something with streams, like this:List<String> owners = jsonPath.get("rows.owner");
// Check that every row has an owner
assertFalse(owners.stream().anyMatch(owner -> owner == null));
// Check that every owner is myself
assertFalse(owners.stream().anyMatch(owner -> !owner.equals("myself")));But for the case where you want to check that every row has an owner, why not just do this?response.body("rows.owner", everyItem(is(notNullValue()));