test json of nested object

1,482 views
Skip to first unread message

Jon Anders Sollien

unread,
Sep 17, 2013, 10:37:23 AM9/17/13
to rest-a...@googlegroups.com
My API returns this json object:

{
"Car": 
       [
{
"id": "2",
"Window": {
"id": "4"
}
}, 

{
"id": "7",
"Window": {
"id": "9"
}
}
]
}

, by using this method: 

expect().get(url)

It's okay to test if there exists a  "Car" with id=2:

expect()
.body("Car.id", hasItem(2) )
.get(url)

I can also test whetever there is a Window with id=4: 

expect()
.body("Car.Window.id", hasItem(4) )
.get(url)

But how can I test that it exists a Car with id=2 which contains a Window with id=4, without knowing the order of the Car-list?

Any help appreciated

Johan Haleby

unread,
Sep 18, 2013, 2:14:30 AM9/18/13
to rest-a...@googlegroups.com
Hi, 

Here you have to make use of Groovy GPath expressions, something like: 

expect().body("Car.find { it.id == '2' }.Window.id", equalTo("4")).when().get(url);

See this blog for some more info.

/Johan



--
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/groups/opt_out.

Jon Anders Sollien

unread,
Sep 18, 2013, 3:45:15 AM9/18/13
to rest-a...@googlegroups.com
Works like a sharm. Many thanks

Jon Anders Sollien

unread,
Sep 18, 2013, 4:45:15 AM9/18/13
to rest-a...@googlegroups.com
I have a little more complex example.
Is it possible to have multiple clauses in the find method? 

Lets say that in addition there is added a new car with id=2, with a Window with id=8. Let us assume that the Window object also contains the property "height" with value of "40" (cm). 

How can i assure that it exists a car with id=2 which contains a Window with id=8 and that THIS Window's height is 40?

Again thanks


On Wednesday, September 18, 2013 8:14:30 AM UTC+2, Johan Haleby wrote:

Johan Haleby

unread,
Sep 18, 2013, 6:20:04 AM9/18/13
to rest-a...@googlegroups.com
Yes, something like this:

expect().body("Car.find { it.id == '2' }.Window.find { it.id == '8' }.height", equalTo(40)).when().get(url);


/Johan

Jon Anders Sollien

unread,
Sep 18, 2013, 6:26:11 AM9/18/13
to rest-a...@googlegroups.com
As I read, "find" returns only the first match. That means that if it is another car with the same id before this in the list, it will do the Window check on that one, and fail if the id or height is another. 

The only problem with that is that the car (id=2, Window{id=8, height=40})

Johan Haleby

unread,
Sep 18, 2013, 7:53:05 AM9/18/13
to rest-a...@googlegroups.com
You can use "findAll" if you want to return all that applies.
Reply all
Reply to author
Forward
0 new messages