I am working on REST API test automation with Rest-Assured. For one API I am getting an array like below in response. From that data array I need to check any of the array item has any property "requestRefNo" with value: "Sss/12345637/58"
{
"data": [
{
"requestRefNo": "Sss/12345637/88",
"requestRefType": "AST",
"requestedByCode": "OWR",
"requestedByDesc": "Asset Owner",
"requestedDate": "12/06/2016",
"requestTypeRefNo": "Sss/12345637/SWT/73"
},
{
"requestRefNo": "Sss/12345637/58",
"requestRefType": "AST",
"requestedByCode": "OWR",
"requestedByDesc": "Asset Owner",
"requestedDate": "10/06/2016",
"requestTypeRefNo": "Sss/12345637/SWT/43"
},
....
],
"links": {
"linkDetails": [
],
"empty": true
},
"errors": {
"empty": true,
"errorDetails": [
]
}
}
I have tried like this:
.assertThat().statusCode(200).body("data.requestRefNo", IsArrayContaining.hasItemInArray("Sss/12345637/58"))))
But it is giving the below error:
java.lang.AssertionError: 1 expectation failed.
JSON path data.requestRefNo doesn't match.
Expected: an array containing "Sss/12345637/58"
Actual: [Sss/12345637/58, Sss/12345637/88]
Can anyone give me any idea?
Thanks, Surodip
Got a very simple answer, missed earlier:
...
.body("data.requestRefNo", Matchers.hasItem("Sss/12345637/58"))
.extract().response();
"data.requestRefNo" will return the array of all requestRefNo in the response array like [Sss/12345637/58, Sss/12345637/88] and Matchers.hasItem will check if the value "Sss/12345637/58" exists in that.
Thanks.