How to validate a json schema in rest assured

Steps:

1. Check the response of your api.

2. Use a json schema generator online, to generate a schema for the json you are using

3. In src/test/resources create a .json file and paste that schema

4. Use validatableResponse interface to validate the schema

5. import hamcreshmatchers, 

6.Import jsonschemavalidator to validate the schema

import io.restassured.module.jsv.JsonSchemaValidator;

import io.restassured.path.json.JsonPath;

import org.hamcrest.Matchers;

Response r1=RestAssured.given()

.baseUri(“http://xxx.x.x.1:8090/api/collections/books/records“)

.when()

.get();

ValidatableResponse r2=r1

.then()

.assertThat()

.statusCode(200)

.body(JsonSchemaValidator.matchesJsonSchemaInClasspath(“firstjsonschema.json”));

System.out.println(r2);

r2.body(“items[0].bookname”, Matchers.equalTo(“The Bhagavat Gita”));

Leave a Reply

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