0
public class Response_SchemaValidation 
{
    public static void main(String [] args) throws FileNotFoundException
    {
        System.out.println("This is testing...");
        
        String str ="{\r\n" + 
                "    \"firstname\": \"Sally\",\r\n" + 
                "    \"lastname\": \"Brown\",\r\n" + 
                "    \"totalprice\": 111,\r\n" + 
                "    \"depositpaid\": true,\r\n" + 
                "    \"bookingdates\": {\r\n" + 
                "        \"checkin\": \"2013-02-23\",\r\n" + 
                "        \"checkout\": \"2014-10-23\"\r\n" + 
                "    },\r\n" + 
                "    \"additionalneeds\": \"Breakfast\"\r\n" + 
                "}";
        
        
        //InputStream insp = new FileInputStream("C:\\Users\\ANANDA K R\\eclipse-workspace\\API_Automation_petStore\\Schema_validation_jsonFiles\\Response_SchemaValidation.json");
        //File fl1= new File("C:\\Users\\ANANDA K R\\eclipse-workspace\\API_Automation_petStore\\Schema_validation_jsonFiles\\Response_SchemaValidation.json");
        RequestSpecification reqstBody = 
                RestAssured.given().body(str).header("Content-Type","application/json");
            Response resp = reqstBody.post("https://restful-booker.herokuapp.com/booking");
            //resp.js 
            
            System.out.println(resp.statusCode());
            String str2 = resp.getBody().asString();
            System.out.println("str2 ---"+str2);
            Assert.assertEquals(resp.getBody().asString(), matchesJsonSchemaInClasspath("Response_SchemaValidation.json"));
            
        
    }

}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Schema to use cannot be null
    at io.restassured.module.jsv.JsonSchemaValidator.validateSchemaIsNotNull(JsonSchemaValidator.java:270)
    at io.restassured.module.jsv.JsonSchemaValidator.access$300(JsonSchemaValidator.java:75)
    at io.restassured.module.jsv.JsonSchemaValidator$JsonSchemaValidatorFactory.create(JsonSchemaValidator.java:281)
    at io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchema(JsonSchemaValidator.java:166)
    at io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath(JsonSchemaValidator.java:117)
    at basic_SchemaValidation.Response_SchemaValidation.main(Response_SchemaValidation.java:173)

Not able to validation schema using Java.... please can someone help to move it forward and complete it?

1 Answer 1

0
    @Test
    public void jsonSchemaTest() {
    String payload = "{\n" +
                "    \"firstname\" : \"Jim\",\n" +
                "    \"lastname\" : \"Brown\",\n" +
                "    \"totalprice\" : 111,\n" +
                "    \"depositpaid\" : true,\n" +
                "    \"bookingdates\" : {\n" +
                "        \"checkin\" : \"2018-01-01\",\n" +
                "        \"checkout\" : \"2019-01-01\"\n" +
                "    },\n" +
                "    \"additionalneeds\" : \"Breakfast\"\n" +
                "}";

                given().
                contentType(ContentType.JSON).
                body(payload).
                when().
                post("https://restful-booker.herokuapp.com/booking").
                then().
                log().
                body().
                assertThat().
                statusCode(200).
                body(JsonSchemaValidator.matchesJsonSchemaInClasspath("ResponseSchema.json"));
    }

ResponseSchema.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "bookingid": {
      "type": "integer"
    },
    "booking": {
      "type": "object",
      "properties": {
        "firstname": {
          "type": "string"
        },
        "lastname": {
          "type": "string"
        },
        "totalprice": {
          "type": "integer"
        },
        "depositpaid": {
          "type": "boolean"
        },
        "bookingdates": {
          "type": "object",
          "properties": {
            "checkin": {
              "type": "string"
            },
            "checkout": {
              "type": "string"
            }
          },
          "required": [
            "checkin",
            "checkout"
          ]
        },
        "additionalneeds": {
          "type": "string"
        }
      },
      "required": [
        "firstname",
        "lastname",
        "totalprice",
        "depositpaid",
        "bookingdates",
        "additionalneeds"
      ]
    }
  },
  "required": [
    "bookingid",
    "booking"
  ]
}

Keep the schema file in src/test/resources. Generate schema online based on your response using this website:https://www.liquid-technologies.com/online-json-to-schema-converter. After that store schema(ResponseSchema.json) in class path and add Json Schema Validator dependency as well. Link:https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator. Official Documentation:https://github.com/rest-assured/rest-assured/wiki/Usage#json-schema-validation

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. for the code,, It is in BDD format.. But am looking for normal selenium format.. In normal selenium format this body is not taking any paramters. for validating json.
Rest assured has got no connection with Selenium. Rest assured is a Java DSL for API testing. Selenium is for automating browsers. You are mixing these two in wrong manner. The above answer is correct. Just update the api end point and pass the payload in body. This code will work.
I have updated the answer with correct payload and endpoint also checked from my side. It is working. I cannot help you more. This is the correct way of doing schema validation in rest assured.
Thank you, i too tried the same ,, it is working..
Hi @ReddyAnand if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.