When we are working with REST or HTTP based service API, we have the responsibility or we can say that it could come as a requirement to validate the behavior of the API. This we perform by validating the schema. For those who are not clear or familiar with what schema validation is? Schema validation ensures that the response coming back from the endpoint matches with the predefined set of rules. Note, that here we are not concerned about the data(values). Rest Assured provides the capability to test this feature using ‘Rest Assured Schema Validator’ and makes it very easy to test. In this example, we will learn “how to perform JSON schema validation with Rest-Assured?”.

 

Simple representation of validating JSON Schema with RESTAssured...!!! Click To Tweet

Steps to follow:

Step 1. Adding ‘Rest Assured Schema Validator‘ to the project

In order to perform this feature testing, first we need to setup the project environment by adding the required dependency to it. So, let’s add below ‘Rest Assured Schema Validator’ maven dependency in your project POM.

Note: Considering that you have already setup the maven project. But, if you are just using plain java project then download the required dependency jar file from this link and add it to the project classpath. See below screenshot:

Json_Schema_Validator_Jar_Rest_Assured_Techndeck

 

Step 2. Generate a sample schema to test

Now, in order to test schema, we need to have one. right? Well, it’s pretty simple, just copy the JSON returned by any of your existing API and generate the schema using any Online schema generator tool like this.
Well, for testing purposes, we have below JSON file and using this above-mentioned tool, we will generate the schema.

Note: Schema is generally provided by developers to test.

JSON:

JSON Schema:

Now, save this schema in a JSON format file. let’s say “JsonSchemaFile.JSON and add it into the classpath of your project.

 

Step 3. Test Code to perform the JSON schema validation with Rest Assured

Finally, let’s jump on the code:

As per the above code, the “matchesJsonSchemaInClasspath” method available in the newly added ‘Rest Assured Schema Validator’ API will help to test the structure of the JSON file passed as an argument here (in our case JsonSchemaFile.json).

 

That’s it, it’s that easy to perform JSON schema validation with REST Assured API 🙂

Simple representation of validating JSON Schema with RESTAssured...!!! Click To Tweet

 

If you like this post, please check out my other useful blog posts on Rest Assured:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

close

Enjoy this blog? Please spread the word :)

Follow by Email
Twitter