Search code examples
jsonrest-assuredmatcherhamcrest

What's the most efficient way of making hamcrest assertions for a JSON object with many keys?


I am making assertions on a RestAssured's ValidateableResponse class (i.e., xxxyyzz.then().body(matchers)). Here, I have many JSON key-value pairs in this response and I can use .then().body("path",hasKey(operator)) method for each JSON key, however, this is very tiresome. Is there any other,more efficient, way to validate multiple JSON keys?

best,


Solution

  • If you need to validate a list/array of some entities, you can do the following:

       response
          .getBody()
          .jsonPath()
          .getList("data.owner_type")
          .forEach(ownerType -> hasKey("public"));