Smoke Test with Json Schema

Asiye Nur Kelle
4 min readMar 29, 2023

--

Introduction

Smoke tests are used to test the core functions of software. The purpose of these tests is to verify that the most important functions of the software are working correctly.
JSON schema validation checks whether a JSON document conforms to the rules defined by a schema. This process checks if the values in the JSON document have the correct data types, whether certain fields are required, and whether other conditions are met.
During smoke tests, JSON documents are often used as software output. Therefore, JSON schema validation is used to verify that the JSON documents used during smoke tests are correctly configured and in the expected format.
In this article, we will inspect a json example, the json schema for this example, and how to write a smoke test with this json schema.

First of all, let’s start with a json example.

This example includes a person’s name, age, city, interests, and education. Interests are represented as a series and educational information as an object.
However, by looking at the json above, you cannot know for sure whether the age field is required or whether the id field can be 0 or not. Exactly at this point, the json schema of this json document makes our life easier.

What is Json Schema

JSON schema is an IETF standard that defines a specific data structure and accuracy in JSON data format. JSON schema can check the structural integrity of data, detect errors, and check the correctness of data types.

If we look at the example diagram above, a person specifies the structure of their data. The schema specifies the properties and data types required for JSON data. For example, the “name” and “city” attributes must be required strings. The “age” attribute must be an integer and at least 0. The “interests” property must be an array. The “education” attribute must be an object, and the “degree”, “major” and “university” attributes in it must be a required array.

So, what are the other keywords we use in json schemes?

JSON Schema Keywords and Properties

We have different keywords that are used in the above example. The following list explains what each of these keywords means.

  • $schema: States that this schema complies with v4 of the IETF standard
  • $id: Defines the base URI to resolve other URI references within the schema.
  • title: Describes the intent of the schema.
  • description: Gives the description of the schema.
  • type: Defines the type of data.
  • properties: Defines various keys and their value types within a JSON document.
  • minimum: Defines the minimum acceptable value for a numeric datatype.
  • items: Enumerates the definition for the items that can appear in an array.
  • minItems: Defines the minimum number of items that should appear in an array.
  • uniqueItems: Enforces if every item in the array should be unique relative to one another.
  • required: Lists the keys that are required and mandatory.

JSON Schema Implementation on the test framework

At this stage, there are many json schema libraries. Since we will use the library below, we need to add the necessary dependencies to the pom.xml file as shown.

We have two java classes that will be used First, JsonSchemaUtils.java contains JSON schema validation, and the second is MediumTest.java which contains the API testing code.

This class has a public method called checkJsonSchema. This method takes two parameters. The first is the file path of the JSON schema and the second is the JSON data itself to be validated.

The method performs these steps:

  • Creates a new JSONObject object.
  • Loads the schema file and creates a Schema object.
  • Validates JSON data using the schema object.
  • If an error occurs, it prints the error message and throws a ValidationException.
  • If there are no errors, it returns a JSONObject.

Let’s take a look at what we did during the test.

  1. We define the API endpoint.
  2. We make API request to “https://example.com/api/person" and get json response.
  3. We compare the JSON data of the response with the uploaded JSON schema file and check its accuracy.

At the end of all this, if the json schema we created and the returned response json value of the api match, we expect our tests to pass.

If the response value as a result of the api request does not match the features and restrictions we have given in the json schema, the test will fail. In this way, we have the chance to notice the smallest change in the API early.

I hope I was able to explain how you can use json schema validation effectively in your tests. Happy reading.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Asiye Nur Kelle
Asiye Nur Kelle

Written by Asiye Nur Kelle

Software Developer in Test @Trendyol

No responses yet

Write a response