Rest Assured Testing Framework

Posted by: admin September 19, 2023 No Comments
Rest Assured testing

Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain.

Our testing framework integrates the Rest Assured library with Java project along with other essentials like TestNG framework, logging, reporting, etc. You can kick-off any rest-assured java project using this framework easily.

Technology and plugins used

This framework uses the following technology stack :

Project Structure

Our testing framework project consists of the following classes :

  • – This is the java class where all the test cases are written that need to be executed. Currently, we have only one java file here, but based upon your requirements you can create multiple java files here to create a test suite. Please note, you should avoid hard coding any test data in these java classes, for example – API endpoints, parameter queries, etc. This java class should only contain the call to relevant test methods and the assertions.
  • – With this java class, we define all the test data that we need like API endpoints, query parameters, methods to call each API endpoint, etc. Please note, there should be no assertions in this java class. It is meant only for defining the test data and methods.
  • – This java class primarily consists of two methods. One is a generic method for sending a GET request and the other is a generic method for sending a POST request. Each method returns the Response object as an argument. You can add more generic methods here if you want to include more types of requests like PUT etc.

How to write test cases

Each test case in this framework should contain a call to a method that should be defined in class and it should return the Response (Json) as the output. The test case should parse the Response object and apply assertions where ever required.

Let’s the first test from the framework as an example.

@Test (priority=1, description="Testing post request by fetching the GSE queue data.")
public void TestPostRequest(){
log.debug("Sending the post request to the API");
Response response = api.postFetchQueue();
log.debug("Evaluating assertions on the fecthed response after sending post request to API");
Assert.assertEquals(response.getStatusCode(), 200);

Let’s start with the explanation of each line in the test case code –

  1. @Test (priority=1, description=“Testing post request by fetching the GSE queue
    This is the general annotation for TestNG.@Test denotes that this is the test case method and should be executed as a test case.
    priority=1 is the test case priority during execution. Higher priority test cases are executed first.
    the description is the test case description which will appear in a report next to the test case name.You can refer to the following link for some basic TestNG annotations:
  2. log.debug(“Sending the post request to the API”);
    This is the method for Log4J module to add a debug log.
    To know more about logging methods of Log4J, please refer:
  3. Response response = api.postFetchQueue();
    Here we are making a call to the methods postFetchQueue() which is defined in class. ‘api’ is an object of class. This method makes a POST request to the fetch queue endpoint of our API. And the response returned by this method is collected to the Response type object which is a class defined in Rest Assured libraries.
  4. Assert.assertEquals(response.getStatusCode(), 200);
    This is an assertion where we are checking the API request’s response status code which should be 200 for a successful call to the API endpoint.

How to write API request method in

Let’s the postRequest() method as an example from

public Response postRequest(String endpoint, JSONObject jsonData)
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
Response response =;
return response;
  1. public Response postRequest(String endpoint, JSONObject jsonData)
    This method takes two arguments, one is of String type and contains the API endpoint, other is of JSONObject type and contains the JSON data which is required to be attached with the POST request.
  2. RequestSpecification request = RestAssured.given();
    This is a generic statement for the Rest Assured library for specifying the specifications. This should not be changed.
  3. request.header(“Content-Type”, “application/json”);
    This statement adds the header for the POST request which should be “application/JSON” as the content type for our example because we are attaching the JSON data with the POST request.
  4. request.body(jsonData.toJSONString());
    Here we are attaching the Json data compiled in the previous step to the request body as a JSON converted to string.
  5. Response response =;
    With this statement we are finally making a POST request call to the desired endpoint. Note that we have already attached the headers and the Json data to the request object in step-3 and 4.
    The response is collected in the Response type object and returned back.

How to run the test suite

First, we need to install Maven on our system. Please follow the below-mentioned guide to install Maven on Windows system:

Once installed successfully, open command prompt as an administrator and navigate to the root directory of your project and execute the following command :

mvn clean test

It should first resolve all the dependencies and download all required JARs. Then the test execution should start.

How to view the Allure report of test execution

To view the Allure report, we first need to install Allure command-line tool on our system using the following guide:

Once installed, please execute the following command in command prompt as an administrator :

allure serve allure-results

This command should open the allure report in a new browser.

Leave a Reply