REST Assured uses a separate module called json-schema-validator. You add it as a dependency, place your schema files in the classpath, and validate with a single line. It is the cleanest schema validation you will ever write.
<!-- Add to your pom.xml dependencies -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>Add json-schema-validator dependency to pom.xml
Create a "schemas" folder inside src/test/resources/
Place your JSON Schema files in this folder (e.g., user-schema.json)
Import matchesJsonSchemaInClasspath in your test class
Use .body(matchesJsonSchemaInClasspath("schemas/user-schema.json")) in your test
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["id", "name", "username", "email"],
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"name": {
"type": "string",
"minLength": 1
},
"username": {
"type": "string",
"minLength": 1
},
"email": {
"type": "string"
},
"phone": {
"type": "string"
},
"website": {
"type": "string"
},
"address": {
"type": "object",
"required": ["street", "city", "zipcode"],
"properties": {
"street": { "type": "string" },
"suite": { "type": "string" },
"city": { "type": "string" },
"zipcode": { "type": "string" },
"geo": {
"type": "object",
"properties": {
"lat": { "type": "string" },
"lng": { "type": "string" }
}
}
}
},
"company": {
"type": "object",
"required": ["name"],
"properties": {
"name": { "type": "string" },
"catchPhrase": { "type": "string" },
"bs": { "type": "string" }
}
}
}
}import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
public class SchemaValidationTest {
private static final String BASE_URL = "https://jsonplaceholder.typicode.com";
@Test
public void testSingleUserSchema() {
given()
.baseUri(BASE_URL)
.when()
.get("/users/1")
.then()
.statusCode(200)
.body(matchesJsonSchemaInClasspath("schemas/user-schema.json"));
}
@Test
public void testAllUsersSchema() {
// For array responses — schema root type should be "array"
given()
.baseUri(BASE_URL)
.when()
.get("/users")
.then()
.statusCode(200)
.body(matchesJsonSchemaInClasspath("schemas/users-array-schema.json"));
}
@Test
public void testPostSchema() {
given()
.baseUri(BASE_URL)
.when()
.get("/posts/1")
.then()
.statusCode(200)
.body(matchesJsonSchemaInClasspath("schemas/post-schema.json"));
}
}Sometimes you want the schema right in the test code. REST Assured supports matchesJsonSchema() for inline schemas.
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchema;
@Test
public void testInlineSchema() {
String schema = "{"""
{
"type": "object",
"required": ["userId", "id", "title", "body"],
"properties": {
"userId": { "type": "integer" },
"id": { "type": "integer" },
"title": { "type": "string" },
"body": { "type": "string" }
}
}
""";
given()
.baseUri("https://jsonplaceholder.typicode.com")
.when()
.get("/posts/1")
.then()
.statusCode(200)
.body(matchesJsonSchema(schema));
}Always prefer matchesJsonSchemaInClasspath() over inline schemas. External schema files are reusable, version-controlled, and easier to maintain. Inline schemas are only good for quick prototyping.
src/
├── test/
│ ├── java/
│ │ └── com/example/tests/
│ │ └── SchemaValidationTest.java
│ └── resources/
│ └── schemas/
│ ├── user-schema.json
│ ├── users-array-schema.json
│ ├── post-schema.json
│ └── comment-schema.jsonThe schema file MUST be in src/test/resources/ (or a subfolder). If you put it in src/main/resources/, matchesJsonSchemaInClasspath() will not find it. This is the most common error new REST Assured users make.
Q: How do you implement schema validation in REST Assured? Where do you place the schema files?
A: Add the json-schema-validator dependency to pom.xml. Place JSON Schema files in src/test/resources/schemas/ folder. In the test, use .body(matchesJsonSchemaInClasspath("schemas/filename.json")) after the status code assertion. REST Assured loads the schema from the classpath and validates the response body against it. For inline validation without a file, use matchesJsonSchema(schemaString). The classpath approach is preferred because schema files are reusable and version-controlled.
Key Point: REST Assured uses matchesJsonSchemaInClasspath() for file-based validation. Schema files go in src/test/resources/schemas/. One line validates the entire response structure.