I have used rest assured many times in my previous positions and with big test suites. Generally, you serialize and deserialize objects with JSON to manipulate the objects. Instead of doing all of that conversion, you can also just use Gpath which can extract and filter JSON with one simple expression. If you are doing a lot of manipulation and extraction to the data then I would suggest using serialization/deserialization if you are just grabbing one value for an assertion, use Gpath.
Just to be clear …..
RestAssured Library is not using Jayway JsonPath and instead uses Groovy’s Gpath, so any documentation on Jayway JsonPath is not going to work. I found documentation and examples on Gpath lacking on Google hence this article.
Toy Api
Lets say we have a simple API for returning information about toys.
{ "Toy": { "name": "Buzz Lightyear",
"Type": "action figure",
"production company": "Disney", "Materials": {
"body": "Plastic"
"arms": "Ballistic Plastic "
} }
Find Single Attribute
After retrieving the response object using the method “.path” will utilize Gpath and then you need to add in the expression. Below we are simply going into the toy node, grabbing the name and then asserting.
Response response = given()
.get("http://toyapi.com")
.then()
.statusCode(200)
.extract().response();
Assert.assertEquals("Buzz Lightyear", response.path("Toy.name"));
Find a Single Attribute on a Specific Node
Generally, you will get more complicated scenarios where you will have a list of items and you want to assert one in particular based on criteria.
{
"Toys": {
"Toy": [
{
"name": "Buzz Lightyear",
"Type": "action figure", "recommendedAge": "12" "productionCompany": "Disney"
},
{
"name": "Optimus Prime",
"Type": "lego", "recommendedAge": "10"
"productionCompany": "Hasbro"
}
]
}
Say you have the above JSON from a toy API and you need to extract the type from Optimus Prime. The below code will filter and take out the type.
String toyName = "Optimus Prime"
Response response = given()
.get("http://toyapi.com")
.then()
.statusCode(200)
.extract().response();
Assert.assertEquals("lego", response.path("Toys.Toy.find {it.name == \"" + toyName +"\" }.Type");
Within the Toys block, go into the Toy which has the name equivalent to “Optimus Prime” then find the type.
response.path(“Toys.Toy.find {it.name == \”” + toyName +”\” }.Type”)
Collect all attributes from a list of nodes
If we wanted to collect all the names from the toy list then we could simply use collect.
response.path(“Toys.Toy.collect { it.name }”);
To use in a test we can simply create an arraylist with the names and perform an assertEquals on both.
Response response = given()
.get("http://toyapi.com")
.then()
.statusCode(200)
.extract().response();
List<String> toyNames = new ArrayList<String>();
toyNames.add("Optimus Prime");
toyNames.add("Buzz Lightyear");
Assert.assertEquals(toyNames, response.path("Toys.Toy.collect { it.name }");
In this example, I gave some brief examples of how to use Gpath with the rest assured library.
Let me know if you found this useful and if you have any questions let me know in the comments!
