Search code examples
rest-assured

how to check the json response array contains the substring


how to assert the json response array contains a substring in a string value.

I have tried this but assertion is failed response.then().body("response.content.Date", Matchers.everyItem(contains("2019-03") ));

my response body is

{
  "response": {
    "totalSize": 2,
    "content": [
      {
        "requestId": " 931-f8222e",
        "name": "gowtham",
        "date": "2019-03-06",
        "issue": "i have a cause"
      },
      {
        "requestId": " 931-f8222e",
        "name": "tharun",
        "date": "2019-03-09",
        "issue": "has a issue in billing"
      }
    ]
  }
}

i want to get all the records in the month(value) and assert the response showing data for given month


Solution

  • I found the solution. You were very close. Instead of using contains which matches full string, you can use containsString to match a substring.

    Code:

    response.then().body("response.content.date", Every.everyItem(Matchers.containsString("2019-03")));