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...!!! Share on X

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.

<dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>json-schema-validator</artifactId>
   <version>4.0.0</version>
</dependency>

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:

[

    {
        "postId": 1,
        "id": 1,
        "name": "id labore ex et quam laborum",
        "email": "Eliseo@gardner.biz",
        "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
    },
    {
        "postId": 1,
        "id": 2,
        "name": "quo vero reiciendis velit similique earum",
        "email": "Jayne_Kuhic@sydney.com",
        "body": "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et"
    },
    {
        "postId": 1,
        "id": 3,
        "name": "odio adipisci rerum aut animi",
        "email": "Nikita@garfield.biz",
        "body": "quia molestiae reprehenderit quasi aspernatur\naut expedita occaecati aliquam eveniet laudantium\nomnis quibusdam delectus saepe quia accusamus maiores nam est\ncum et ducimus et vero voluptates excepturi deleniti ratione"
    },
    {
        "postId": 1,
        "id": 4,
        "name": "alias odio sit",
        "email": "Lew@alysha.tv",
        "body": "non et atque\noccaecati deserunt quas accusantium unde odit nobis qui voluptatem\nquia voluptas consequuntur itaque dolor\net qui rerum deleniti ut occaecati"
    },
    {
        "postId": 1,
        "id": 5,
        "name": "vero eaque aliquid doloribus et culpa",
        "email": "Hayden@althea.biz",
        "body": "harum non quasi et ratione\ntempore iure ex voluptates in ratione\nharum architecto fugit inventore cupiditate\nvoluptates magni quo et"
    }

]

JSON Schema:

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    }
  ]
}

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:

import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import org.testng.annotations.Test;
import io.restassured.RestAssured;

public class testJsonSchema {

    @Test
    public void testJsonSchema() {

        RestAssured.given()
            .when()
            .get("https://jsonplaceholder.typicode.com/comments?postId=1")
            .then()
            .assertThat()
            .body(matchesJsonSchemaInClasspath("JsonSchemaFile.json"));

    }
}

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...!!! Share on X

 

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.

    View all posts