How to write API Automated tests — RestAssured (4/7)

In this article we will add API test to the previously created Web automation tests.

Code from this chapter can be found here.

Also read previous article about Web automation or introduction article about test automation framework.

Add Rest Assured dependencies

As an API client, we will use a java library called RestAssured.

We will add two dependencies:
rest-assured — core RestAssured library
json-schema-validator — matcher, used to compare JSON response with the defined JSON schema

Check this repository for the latest library version.

Add code below to the pom.xml file:

Add feature file

Add api package to the features package and create api_requests.feature file:

As an application under test we will use jsonplaceholder.typicode.com. Free to use fake online REST API for testing and prototyping. We will perform requests to the /users, /posts and /albums endpoints, verify response code and response body.

Scenario API Client is initialized will read host URL from the configuration file.

Add code below to the api_requests.feature:

Add glue code

We start from the adding target system URL above to the test configuration.

In the each *.properties file from the env_config folder replace dummy host value by https://jsonplaceholder.typicode.com/

host=https://jsonplaceholder.typicode.com/

Create RequestsSteps class inside steps package and add code below:

For the responseBodyIsMatchedToSchemaJson step we need to add scema.json file to the resource folder:

This file describes each fields in a JSON file and used for the JSON body validation. Service jsonschema.net can be used for the JSON schema generation based on a provided JSON body.

Add text below to the scema.json file:

Execute the tests

The final version of a code is placed here.

Execute code via running api_requests.feature file. Console output will be:

We already implement simple API and Web automated tests using Cucumber library. In the next articles we will extend framework functionality by listeners, reports, logs and integrate it with the CI pipeline.

Check the next article and find how to add listeners to the Cucumber test automation framework.

Test automation engineer, writer