RestAssured #18 - Schema Validation
Introduction
When working with APIs, ensuring the integrity and structure of data exchanged between clients and servers is crucial. One effective way to achieve this is through schema validation. Schema validation involves comparing the structure of the API response against a predefined schema or specification, verifying that the data adheres to the expected format. This process helps catch errors, inconsistencies, and unexpected changes in the API's response data.
Benefits of Schema Validation
Schema validation offers several benefits for both API providers and consumers:
Data Consistency: Schema validation ensures that the data being exchanged adheres to a consistent structure, reducing the chances of data integrity issues.
Early Detection of Issues: Schema validation can catch errors in the API response early in the development cycle, making debugging and troubleshooting more efficient.
Version Compatibility: As APIs evolve, schema validation helps ensure that newer versions of the API still comply with the original schema, avoiding compatibility problems.
Clear Communication: By defining a schema, API providers communicate the expected structure to consumers, promoting understanding and correct usage.
Dependency
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>5.3.1</version>
</dependency>
Create Schema
Endpoint - https://test-api.k6.io/public/crocodiles/
Generate schema of Json response using https://jsonschema.net/app/schemas/0
{
"type": "array",
"default": [],
"title": "Root Schema",
"items": {
"type": "object",
"default": {},
"title": "A Schema",
"required": [
"id",
"name",
"sex",
"date_of_birth",
"age"
],
"properties": {
"id": {
"type": "integer",
"default": 0,
"title": "The id Schema",
"examples": [
1
]
},
"name": {
"type": "string",
"default": "",
"title": "The name Schema",
"examples": [
"Bert"
]
},
"sex": {
"type": "string",
"default": "",
"title": "The sex Schema",
"examples": [
"M"
]
},
"date_of_birth": {
"type": "string",
"default": "",
"title": "The date_of_birth Schema",
"examples": [
"2010-06-27"
]
},
"age": {
"type": "integer",
"default": 0,
"title": "The age Schema",
"examples": [
13
]
}
},
"examples": [{
"id": 1,
"name": "Bert",
"sex": "M",
"date_of_birth": "2010-06-27",
"age": 13
}]
},
"examples": [
[{
"id": 1,
"name": "Bert",
"sex": "M",
"date_of_birth": "2010-06-27",
"age": 13
}]
]
}
Here's a breakdown of the components of this JSON schema:
type: Specifies that the root element of the JSON should be an array.
default: Provides a default empty array.
title: A human-readable title for the schema.
items: Describes the schema for the individual objects within the array.
type: Specifies that the array contains objects.
default: Provides a default empty object for each individual object.
title: A title for the individual object schema.
required: An array listing the properties that are required for each individual object.
properties: Defines the properties of each individual object.
- Each property has a type, default value, title, and example values.
examples: Contains an example array of individual objects that conform to the defined schema.
Validating schema in Rest Assured using JsonSchemaValidator
Add schema under the test resources folder
@Test
public void crocodilesSchemaTest(){
RestAssured.baseURI = "https://test-api.k6.io";
RestAssured.given()
.when()
.get("/public/crocodiles/")
.then().log().all()
.assertThat()
.body(JsonSchemaValidator.matchesJsonSchemaInClasspath("crocodiles_schema.json"));
}
Subscribe to my newsletter
Read articles from SUBODH SINGH directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
SUBODH SINGH
SUBODH SINGH
An Automation Engineer, experienced in the automation of front-end and back-end applications with 11+ years of experience. Experience in designing, developing, and implementing automation frameworks. I like to learn and share knowledge on test automation.