JSON Schema Validation in REST Assured

Profile picture for user devraj

Schema validation ensures that the response coming back from the endpoint matches with the predefined set of rules. So, without analyzing the response in detail, to know first-off whether the JSON body conforms to a certain JSON format. 

JsonSchemaValidator, A Hamcrest matcher that can be used to validate that a JSON document matches a given JSON schema. From version 2.1.0 REST Assured has support for Json Schema validation. 

Step 1: Add following dependency in pom.xml. You can check latest maven dependency from here.

<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->

Step 2: Then we need schema for test, which is generally provided by the developer but if it is not you can generate schema from here using json response.

Step 3: Save generated schema to .json file.

Step 4: Paste below code

public void jsonschematest()
        .pathParam("id", id)

Step 5: If you are getting error do static import: