Structural Validation of JSON Using Rest Assured
In this article, we discuss JSON Schema Validation and how Rest Assured can be integrated with BDD frameworks like Cucumber so we can write BDD style code.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
Rest Assured is an open-source Java-based library used to test RESTful APIs. Using Rest Assured, we can test XML and JSON-based web services. It behaves like a headless client to access APIs and interact with them. It supports different HTTP methods like GET
, POST
, PUT
and DELETE
. It can be integrated with popular testing frameworks like Junit, TestNG, etc.
Rest Assured can be integrated with BDD frameworks like Cucumber and we can write BDD style code. Rest Assured supports the Gherkin syntax (Given/When/Then) making the tests human-readable and easy to understand. Rest Assured is implemented in a language called "Groovy."
Schema/Structure Validation
Upon receiving a valid HTTP request in the form of URL/URI from the client-side, the server returns a JSON response. Validating the response received from the server is a vital task in API Testing. While we talk about testing the response body, we can break the testing into 3 areas:
- Structural/Schema Validation
- Response Body Validation
- Individual Element Validation
In this article, we will see how to perform Structural or Schema Validation of a JSON response.
Let us consider that this is our Response JSON:
{
"id": 10,
"firstName": "Remi",
"lastName": "Jullian",
"email": "remi.jullian@abc.com",
"programme": "Financial Analysis",
"courses": [
"Accounting",
"Statistics"
]
}
For Schema validation, we need to create a schema for our JSON. This will act as our expected schema for validation. We can manually create the schema or we can use any online JSON schema generators to create our expected schema.
Schema is nothing but a JSON file. It will have only datatype information and the expected keys of the JSON. There won't be any values present in the schema.
The expected schema for the above JSON is seen below:
xxxxxxxxxx
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"email": {
"type": "string"
},
"programme": {
"type": "string"
},
"courses": {
"type": "array",
"items": [
{
"type": "string"
},
{
"type": "string"
}
]
}
},
"required": [
"id",
"firstName",
"lastName",
"email",
"programme",
"courses"
]
}
Rest Assured Logic To Test the Schema
Step 1: Add a "JSON schema validator" dependency in pom.xml.
xxxxxxxxxx
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.0</version>
</dependency>
Step 2: Add a "hamcrest-all" dependency for asserting the JSON schema.
xxxxxxxxxx
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
Step 3: Load the expected "schema.JSON" in a file object.
x
File schema = new File(System.getProperty("user.dir")+"schema.json");
Step 4: Fire the rest assured request, validate the response body using the matchesJsonSchema
method.
x
given().get("http://localhost:8080/student/10").then().body(matchesJsonSchema(schema));
The matchesJsonSchema
method validates the response body matches the structure—data type and the required keys. If there is any mismatch, it will report in the console.
Full Code
xxxxxxxxxx
import static io.restassured.RestAssured.*;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchema;
import java.io.File;
import org.testng.annotations.Test;
public class StructureValidation {
public void JSONSchemaValidation() {
File schema = new File(System.getProperty("user.dir")+"schema1.json");
given().
get("http://localhost:8080/student/10").
then().
body(matchesJsonSchema(schema));
}
Step 5: Schema validation programmatically is very useful when you have a nested JSON, which is very time consuming if we need to validate manually.
Conclusion
In this article, we discussed JSON structure assertion. Validating the structure of the response JSON programmatically is very useful when you work with complex nested JSON's, where testing manually is very time consuming.
Opinions expressed by DZone contributors are their own.
Comments