I have a JSON response to validate. I am writing a test secario where I want to assert if the response contains the number of objects or not. JSON response:
{
"Result": {
"resultCode": "1000",
},
"ResultClient": {
"responseCode": null,
"statusCode": null
},
"creditCard": {
"number": null
}
}
I want to assert that the response has 3 objects. How to do that? The response obj dosn't have size()
or count()
so I am unable to understand the path to the solution.I am writting my tests in rest-assured .
TestResponse testResponse = given()
.contentType("application/json; charset=UTF-8")
.body(cTestRequest)
.when()
.post(uri)
.as(TestResponse.class);
now how to assert the json contains the 3 obj and the parameters inside the objs?
You can do something like this:
The reason is that the JSON object is treated as Groovy
Map
so you can invoke functions on it.keySet()
returns all keys as aSet
andsize()
returns the size of thisSet
.