How to assert based on body item with Rest Assured with JSON response?

JJB picture JJB · Feb 26, 2018 · Viewed 12.5k times · Source

How can I assert my properties inside the "description" array using the rest assured .body() method.

Example:

 .body ("[0] .userType", equalTo (1)); // error 

Here is my current JSON data which I want to assert with:

{
"validSession": true,
"value": "xxx",
"description": [
    {
        "userType": 1,
        "userTypeDescription": "xxx",
        "uname": "xx",
        "distributorId": 1
    }
]}

Answer

JJB picture JJB · Feb 26, 2018

I dit it:

.body("validSession",is(true))
.body("description[0].userType", equalTo(1))
.body("description[0].userTypeDescription", containsString("xxx"))
.body("description[0].uname", containsString("xx"))
.body("description[0].distributorId", equalTo(1));

I tested and it worked. but I did not understand why it only worked by putting all elements of the array with index zero.

Can you explain?