4

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
    }
]}
anurag0510
  • 763
  • 1
  • 8
  • 17
JJB
  • 115
  • 1
  • 1
  • 7

4 Answers4

6

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?

JJB
  • 115
  • 1
  • 1
  • 7
0

Try using the following code snippet :

.body("description[0]", hasItem(1))

Let me know if it was helpful.

anurag0510
  • 763
  • 1
  • 8
  • 17
0

Can you explain?

The reason you needed to reference description[0] in your test is that the element "description" in your JSON data is an array. You're using array syntax to declare your intent to read the first element of the array named "description".

dugsmith
  • 119
  • 3
  • 9
0

You can also use Hamcrest:

Response data = httpClientRequest.getApiCall(url);
data.then().assertThat().body("description.userTypeDescription[0]", Is.is("xxx"));
Artog
  • 1,132
  • 1
  • 13
  • 25