#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Rest Assured

Verifying Response Json Schema Using Restassured

What is Schema Validation?

Rather than checking individual field values, schema validation confirms that the overall structure of a JSON response (field names, types, required fields) matches an expected schema definition.

Example

.then().assertThat()
    .body(matchesJsonSchemaInClasspath("user-schema.json"));

Why This Matters

Schema validation catches structural regressions — like a field unexpectedly becoming null, changing type, or disappearing entirely — that individual field assertions might miss if not written for every field.

Setting Up Schema Validation

Schema checks need a separate REST Assured module. Add it to your pom.xml next to the main library, using the same version number:

<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>json-schema-validator</artifactId>
  <version>${rest-assured.version}</version>
  <scope>test</scope>
</dependency>

Place schema files under src/test/resources, for example in a schemas folder. Anything in that directory is on the test classpath, which is exactly where matchesJsonSchemaInClasspath looks.

Writing the Schema and the Test

Here is a schema for a user object. It lists required fields, gives each a type, allows phone to be missing or null, and rejects unexpected fields:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "required": ["id", "name", "email", "active"],
  "properties": {
    "id":     { "type": "integer" },
    "name":   { "type": "string", "minLength": 1 },
    "email":  { "type": "string" },
    "active": { "type": "boolean" },
    "phone":  { "type": ["string", "null"] }
  },
  "additionalProperties": false
}

The test combines the schema check with a couple of value checks. The schema guards the shape; the value assertions guard the meaning.

import static io.restassured.RestAssured.given;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static org.hamcrest.Matchers.equalTo;

public class UserApiTest {

    @Test
    public void userResponseMatchesSchema() {
        given()
            .baseUri("https://api.example.com")
            .accept("application/json")
        .when()
            .get("/users/42")
        .then()
            .statusCode(200)
            .body(matchesJsonSchemaInClasspath("schemas/user-schema.json"))
            .body("id", equalTo(42));
    }
}

Notice the order inside then(): the status code is checked first. If the API returns an error page, you get a clear "expected 200" message instead of a confusing schema failure about missing fields. For an endpoint that returns a list, set the top-level "type" to "array" and describe each element under "items". You can reuse the same object definition there.

Reading Failures and Choosing How Strict to Be

When validation fails, the error message names the schema rule and the JSON path that broke it, such as a missing required property or an id that arrived as a string. That makes the cause much quicker to spot than a generic assertion failure.

The library behind REST Assured's validator works with older JSON Schema drafts, so stick to draft-04 keywords unless you have confirmed newer ones are supported in your setup.

Strictness is a team decision. "additionalProperties": false catches accidental leaks of internal fields, but it will also fail when the API adds a new, harmless field. Many teams use strict schemas for their own services and looser ones for third-party APIs that change without notice.

Common Mistakes

  • Writing a schema with no required list, so an empty object still passes.
  • Generating a schema from one sample response and never reviewing it; generated schemas often allow too much or too little.
  • Forgetting that a field can legitimately be null, causing false failures.
  • Using a wrong classpath path, which fails with a "resource not found" error rather than a schema error.
  • Keeping schema files out of version control, so they drift from the real contract.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course