(how to) Check nested attribut inside an array attribut

1,513 views
Skip to first unread message

William Baker

unread,
Apr 30, 2018, 2:00:12 PM4/30/18
to REST assured
Greetings,

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 matcher Matchers.hasProperty("owner") or even HasPropertyWithValue.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

Todd Bradley

unread,
Apr 30, 2018, 2:41:55 PM4/30/18
to rest-a...@googlegroups.com
Are you using Java 8? Seems like this could be a great use of streams. 


--
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.
--
Sent from Gmail Mobile
Message has been deleted

William Baker

unread,
Apr 30, 2018, 2:44:41 PM4/30/18
to REST assured
Hello,

Yes I'm

Todd Bradley

unread,
Apr 30, 2018, 10:37:43 PM4/30/18
to rest-a...@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()));


On Mon, Apr 30, 2018 at 12:44 PM, William Baker <guillaume....@gmail.com> wrote:
Hello,

Yes I'm

--
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.

Guillaume Boullanger

unread,
May 1, 2018, 6:38:11 AM5/1/18
to rest-a...@googlegroups.com
Thank you Todd.

response.body("rows.owner", everyItem(is(notNullValue())); works fine for me

I was hoping that something along with response.body("rows", everyItem(hasProperty("owner")));  would be possible as well but seems not.

On Tue, May 1, 2018 at 4:37 AM, Todd Bradley <to...@toddbradley.com> wrote:
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()));

Reply all
Reply to author
Forward
0 new messages