REST API testing has gained popularity over time due to its simplicity and ease of use.
REST API regression test automation usually includes status code verifications, parsing response body and comparing the server’s actual response with the expected one. The responses are generally in JSON or XML formats. There are many open source tools making rounds on the internet to perform the automation of the same. Rest Assured is one such tool. Rest assured is a java based tool used for automating Restful web services. It is a powerful behavior driven library set up for testing out various API endpoints and their response bodies. It helps in writing readable, maintainable tests for RESTful APIs. 

Let’s have a look at how Rest Assured can be configured using a Maven project.

Rest assured can be added as a dependency in maven pom.xml as follows:

Use the following static import in your test class.

Let’s take an example for a GET method from https://reqres.in/

This is the expected response:

Here is the sample code for a simple test case for GET :

The script follows a given-when-then format. You can predefine the URI or add it in GET method. The above code hits the get URI and verifies the status code and validated the response body.

Suppose you have to perform an authentication operation you might as well add that using TestNG annotation and set beforehand. Given below is a basic auth operation:

Let’s now take a look at a simple POST operation. Here, I have set the content type as Json and have directly added the Json body for the POST.

Please note that there is also a provision to add the body as a Json file or create a POJO class for the Json and add as a class.

In some cases, we pass values as headers and parameters. These can be accommodated in rest assured as well.

 

To conclude, Rest assured is a flexible tool to test out Rest API and parse their responses. It can also be seamlessly integrated with TestNG or JUnit to add more functionalities. This helps us design an efficient testing framework to anticipate and accommodate changes in functionalities.