Rest Assured Testing Framework
Tags: softwaretestincompany
Published: 2021-10-14 06:41:55

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 :

Java – As a general coding language for writing test cases and methods.
Rest Assured – This is the library that is used to make server requests and fetch responses and process the response for analyzing and assertions.
http://rest-assured.io/
TestNG – TestNG library is used as a testing framework to manage and execute test cases/suites.
https://testng.org/doc/
Log4J – Log4J library is used for generating the project logs.
https://logging.apache.org/log4j/
Allure – Allure is a plugin used to generate beautiful test reports.
http://allure.qatools.ru/
Maven – Maven plugin is used to manage the project dependencies.
http://allure.qatools.ru/
Eclipse – As an IDE, eclipse has been used.
https://www.eclipse.org/downloads/
Project Structure
image1

Our testing framework project consists of the following classes :

Test.java.ApiTest.java – 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.
Test.resources.ApiData.java – 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.
Test.resources.ApiMethods.java – 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 ApiData.java 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 –

@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:
https://www.tutorialspoint.com/testng/testng_basic_annotations.htm
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:
https://www.tutorialspoint.com/log4j/log4j_logging_methods.htm
Response response = api.postFetchQueue();
Here we are making a call to the methods postFetchQueue() which is defined in ApiData.java class. ‘api’ is an object of ApiData.java 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.
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 ApiMethods.java
Let’s the postRequest() method as an example from ApiMethods.java

public Response postRequest(String endpoint, JSONObject jsonData)
{
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
request.body(jsonData.toJSONString());
Response response = request.post(endpoint);
return response;
}
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.
RequestSpecification request = RestAssured.given();
This is a generic statement for the Rest Assured library for specifying the specifications. This should not be changed.
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.
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.
Response response = request.post(endpoint);
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:

https://www.mkyong.com/maven/how-to-install-maven-in-windows/

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:

https://docs.qameta.io/allure/#_installing_a_commandline

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.