How to validate JSON Schema in Rest Assured

 

To validate a JSON schema using Rest Assured in a GET API response, you can use the JsonSchemaValidator provided by Rest Assured. It helps ensure that the structure of the JSON returned from the API matches an expected schema.


















Steps to Use JSON Schema Validation in Rest Assured


1. Add Dependencies

<dependencies>
    <!-- Rest Assured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>

    <!-- JSON Schema Validator -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>





2. Create JSON Schema File

Create a file users-schema.json in the src/test/resources directory.


{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "page": { "type": "integer" },
    "per_page": { "type": "integer" },
    "total": { "type": "integer" },
    "total_pages": { "type": "integer" },
    "data": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "email": { "type": "string" },
          "first_name": { "type": "string" },
          "last_name": { "type": "string" },
          "avatar": { "type": "string" }
        },
        "required": ["id", "email", "first_name", "last_name", "avatar"]
      }
    },
    "support": {
      "type": "object",
      "properties": {
        "url": { "type": "string" },
        "text": { "type": "string" }
      },
      "required": ["url", "text"]
    }
  },
  "required": ["page", "per_page", "total", "total_pages", "data", "support"]
}





3. Java Code to Validate Schema


import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static io.restassured.RestAssured.*;

public class JsonSchemaValidationTest {

    public static void main(String[] args) {

        RestAssured.baseURI = "https://reqres.in";

        given()
            .contentType(ContentType.JSON)
        .when()
            .get("/api/users?page=2")
        .then()
            .assertThat()
            .statusCode(200)
            .body(matchesJsonSchemaInClasspath("users-schema.json"));

        System.out.println("Schema validated successfully.");
    }
}

How to test a SOAP API by Rest Assured


Testing a SOAP API GET request using Rest Assured is possible but requires understanding how SOAP works. However, SOAP typically uses POST, not GET, because it sends a full XML envelope in the body. But if the SOAP service is exposed via GET (rare, usually for testing or WSDL fetching), we can use RestAssured.get().


















Key Concepts:

  • SOAP (Simple Object Access Protocol) is a protocol for exchanging structured XML data.

  • WSDL (Web Services Description Language) is used to describe SOAP services.

  • SOAP API is generally tested using POST, but for some services (like fetching WSDL), GET is used.



Example SOAP GET Use Case:

Fetch the WSDL file or perform a test GET to an endpoint.

Let’s use this public SOAP service for currency conversion WSDL (GET example):
http://www.dneonline.com/calculator.asmx?WSDL


This WSDL can be fetched using a GET request.


Step-by-step: Testing SOAP GET using Rest Assured:



Maven Dependency:

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>





Java code:


import io.restassured.RestAssured;
import io.restassured.response.Response;

public class SoapGetTest {
    public static void main(String[] args) {
        // Base URI for the SOAP service
        String url = "http://www.dneonline.com/calculator.asmx?WSDL";

        // Send GET request
        Response response = RestAssured
                .given()
                .when()
                .get(url)
                .then()
                .extract()
                .response();

        // Print status code and response
        System.out.println("Status Code: " + response.getStatusCode());
        System.out.println("Content-Type: " + response.getContentType());
        System.out.println("Response Body:\n" + response.getBody().asString());
    }
}




Optional: Add Test Assertion


import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class SoapGetTest {
    public static void main(String[] args) {
        given()
        .when()
            .get("http://www.dneonline.com/calculator.asmx?WSDL")
        .then()
            .statusCode(200)
            .contentType("text/xml; charset=utf-8")
            .body(containsString("definitions"));
    }
}