JsonSchemaValidator with RestAssured.

JsonSchemaValidator with RestAssured.

I was checking this very simple but useful part of testing API's defined Schema with API response. This is not part of functional testing but it's a important part of testing contract level testing of API. Most of the time these issues found at the time of integration of two services. And to validate a JSON response based on a predefined JSON schema we can use the JsonSchemaValidator library provided by RestAssured. There are many libraries available that you can check as well, this one seems simple to use and do its job. we will see how you can use that here.

What can we achieve with API Schema Validation:

  1. The Structure of API Response has adhered to the defined contract or not.
  2. The Data type of API's keys adheres to the defined contract, typos, etc.
  3. Whether any key required attribute is missing from API response.

We will take a very small example below know how we can do it using Java :

  1. Assume we have a Get API: https://reqres.in/api/users?page=2 which returns us below data.
{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}

2. Now we want to verify whether all the keys, their datatype, required attributes are present or not in above response, to do that we need a source of truth, here we need to create JSON Schema, Usually we can create this JSON Schema at the time of Contract Definition, but you can also convert later as well and get it reviewed once. Also, the above response can easily be converted into JSON Schema by any Free Online JSON to JSON Schema Converter eg. https://www.liquid-technologies.com/online-json-to-schema-converter

No alt text provided for this image
No alt text provided for this image

3. Now, copy this schema and put it into your classpath with schema.json, which might be in the project where you writing your tests for API.

2. Now, if we want to validate the above-mentioned things we can use JsonSchemaValidator provided dependency by rest assured:

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

3. Now, if you want to validate the above JSON Schema, here is a sample test method that you can use to validate:

import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;

public void validaetJSONSchema() {

    String responsebody = given().baseUri("https://reqres.in/").basePath("/api/users?page=2").get().then().extract().body().asString();
    System.out.println(responsebody);

    assertThat(responsebody, matchesJsonSchemaInClasspath("schema.json"));
}

4. You can try to change the schema.json to validate something, lets change the date type to integer, and your test will find a string in response and your test will fail and show us the reason for failure something like below:

No alt text provided for this image

Now, One important part of JSON validation is the required section: many times we keep some fields optional or required this will allow us to modify the JSON and validation will be done on that as well.

"required": [
            "id",
            "email",
            "first_name",
            "last_name",
            "avatar"
          ]


Conclusion:

JSON response Schema validation is an important part of testing your API response. This will help us validate whether our API response adheres to the contract or not. We can learn more about this on their official website too. Hope you can easily validate your API response with this mini article.

If you know another validator as well share here, I will try to learn that too with this.


Anshiv Dixit

Human Resources Associate at Cloud Hire with expertise in Critical Thinking

1 个月

Hey Anand Do you wanna switch, I have an amazing Job Offer for you for the Job Role of QA Manager Aerospace. Please update me at the Earliest. Eagerly waiting for your Response. Thanks & Regards

回复
Muhammad Osama Saleem

QA Engineer | Automation | API & Web | ISTQB Certified | Matas A/S

2 年

HI, i am facing the issue https://stackoverflow.com/questions/72970368/schema-validation-of-rest-api-serenity-bdd if you know any solution that would be so great. Thanks

回复
Hemant Zope

Senior Quality Test Engineer II at LexisNexis Risk Solutions - API Automation using Karate | UI Automation using Java, Selenium, TestNG

3 年

Thanks for posting

回复
Sunil Bansal

Top Talent FY23 | Test Automation Expert | Sr Training Specialist - Technical

3 年

Well said

回复

要查看或添加评论,请登录

Anand Bhagwat的更多文章