Given I have this JSON array:
{
value: ["000", "111", "345", "987"]
}
I want to use Rest-assured to validate the format of the fields using it's given/when/then structure.
given().
queryParam("myparam", myparamvalue).
when().
get(callRoot).
then().
body("value", matchesPattern("[0-9][0-9][0-9]");
How do I get Rest-assured to loop through and apply the test against each value in the JSON array?
I don't know how many values will be in the JSON array. It might be only 1; it might be 100.
You could use
JsonPath
and do something like the following:See https://github.com/rest-assured/rest-assured/wiki/usage#json-example for more details.
Or you could extract the response as a
String
, transform it to aJSONObject
, extract theJSONArray
in thevalues
field, and then apply the regex to each item in the array: