Warning: fopen(/tmp/SghXTngBZPli-zrN2m4.tmp): failed to open stream: Disk quota exceeded in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 139

Warning: unlink(/tmp/SghXTngBZPli-zrN2m4.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
Site Loader
Auckland, New Zealand

JSON Schema Matcher

JSON Schema matcher is an handy way to quickly verify the JSON response based upon the expected schema format.So, basically Schema validation ensures that the response coming back from the endpoint matches with the predefined set of rules.

JSON Schema Matcher in RestAssured

In order to work JSON schema matcher in RestAssured, we need to install the following package in our POM.xml file (if you are using Maven)

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

Example Matcher

The code that we are going to write for JSON schema matcher is going to look something like this (in cucumber bdd step definition)

 @Then("^I should see the author name as \"([^\"]*)\" with json validation$")
    public void iShouldSeeTheAuthorNameAsWithJsonValidation(String arg0) throws Throwable {

        //returns the body as string
        var responseBody = response.getBody().asString();

        //assert
        assertThat(responseBody, matchesJsonSchemaInClasspath("post.json"));

    }

As you can see we are using matchesJsonSchemaInClasspath method, which is responsible for reading the JSON file from our classpath within project and match if the schema what we have is equivalent to what we get from JSON response.

Here is the complete video of the above discussion

Once again, thank you very much for watching the video and reading the article, if you are interested in the complete video series, please check it out from here

Post Author: Karthik kk

2 Replies to “JSON Schema matcher with RestAssured for API testing”

  1. I saw you just paste the plain JSON data with the JSON file. How is it transfered to JSON schema? Is there any other configuration or some other tools. Because I know the expected JSON file is schema JSON file.

  2. As part of validating schema, won’t you wanna check if the ‘integer’ values are verified correctly? You have changed an integer to string and let it pass. How is that valid?

Leave a Reply

Your email address will not be published. Required fields are marked *