How to Test Rest Services with Rest-Assured Framework

Author: Seda ALTIOKOĞLU ATASOY, SW Test Engineer / Defence SW Applications Group

 

What does “REST” means ?

Rest stands for “REpresentational State Transfer”, that is an architectural style for developing web services. A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.

Restful API’s are stateless, that means; there is no state information is held by application, it is performed by the client. Additionally, there is no information about the client is kept on the server side. You do not have to include a dll, a class or an object into your project on the client side to benefit from the service.

Restful API’s are platform independent, that it does not matter what platform the server and the client are developed with.

Restful API’s mostly work with JSON text format, but it is not just limited to work with JSON, the client can use responses as XML or raw text.

Restful API’s are light-weighted, that means, you do not have any XSD’s or WSDL’s like in SOAP services, you completely free to change request or response type whenever you need.

Let’s look at the operations to be done and their meanings

Rest API’s uses HTTP response codes to communicate with consumers, that is, 200 stands for “OK”, 404 stands for “Not found” and 409 stands for “Conflict” and so on.

What is “RestAssured” ?

RestAssured is a test automation framework that brings all the simplicity of all other scripting languages to Java side. RestAssured is designed to test Rest API’s in an easy and meaningful way by Jayway. The framework supports many rest methods like POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD to validate and verify response.

Rest Assured has a template format like “given / when / then” but for an earlier version of this framework not followed this template, the reason for not using these words in the first place was mainly technical. Before 2.0 version Rest Assured did not support “given / when / then” that is like Behavior Driven Testing (BDD) approach.

Setup of RestAssured

All below explanations are according to Maven project;

Firstly we need to add Maven dependencies to use Rest Assured in our project as

Now, we are able to use all ability of RestAssured. Additionally, we are going to use Hamcrest matchers to perform assertions.

Testing GET methods

 If you are going to test “GET” method that means you are going to verify some data retrieved from the server that already saved before.

Assume that you are going to get user data with given user identity number.

By using Rest Assured methods (most likely BDD like), we can easily retrieve and validate response from corresponding REST API.

Assume that you are going to test something names as “Cost”, steps of this test should be like;

  1. Status of response (200, 404, 500).
  2. Compare retrieved data is matching with saved user profile data (A).

Status of response:

 

According to above code piece, annotations show the feature and severity of this case for our aspect these annotations can be skipped.
Above code piece is retrieving data from the baseUrl + “/cost” by “GET” method and waits statusCode as 200 that means “OK”.

Compare retrieved data is matching with saved data:

    

Above code piece retrieves data from rest API and validates it by using Rest Assured methods. @Test, @Feature, @Severity and @Step annotations are belonged to TestNG (Testing Framework) not Rest Assured.

Testing POST method:



Testing PUT method:

Testing DELETE method:

When you are testing DELETE method, good to verify that you delete the data from database like in @Step(“Step 2 : Check deleted data exist or not”) as a best practice.

Rest Assured library provides a lot of features, DSL-like syntax, X-Path validations, easy to read test data from file, easy to authentication test and so on.

Those features help to automate your test and Rest-Assured framework provide us readable code with a Gherkin type syntax as BDD.

 

Suggested Resources:

  1. http://searchmicroservices.techtarget.com/definition/RESTful-API
  2. https://www.json.org
  3. http://searchmicroservices.techtarget.com/definition/REST-representational-state-transfer
  4. https://github.com/rest-assured/rest-assured/wiki/Usage

 

 

Leave a Reply

Your email address will not be published.