Rest Assured Testing Framework

0
2KB

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

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 –

  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:
    https://www.tutorialspoint.com/testng/testng_basic_annotations.htm
  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:
    https://www.tutorialspoint.com/log4j/log4j_logging_methods.htm
  3. 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.
  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 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;
}
  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 = 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.

Get to know us more on https://www.fleekitsolutions.com/

Pesquisar
Patrocinado
Categorias
Leia mais
Outro
Global Gas Insulated Transformer Market Overview A Year in Review
Introduction: Gas Insulated Transformer Market is exhibiting a compound annual growth rate (CAGR)...
Por Devanand Patil 2023-12-01 06:42:22 0 949
Outro
Does Southwest Have a 24 Hour Refund Policy?
Due to last-minute emergencies, people have to cancel their travel plans and request a refund....
Por Brent Jon 2024-01-05 07:18:06 0 969
Outro
Discovering Teacher Jobs in Uganda : Possibilities in addition to Issues
  Benefits: Uganda, termed as a Pill connected with Africa, contains a loaded national...
Por Fraizer Wall 2024-06-09 22:42:40 0 642
Outro
Best SEO Services in India: Unlocking Growth for Your Business
In today’s fast-paced digital world, where almost every business strives to establish a...
Por SEO Gliders 2024-09-27 09:19:37 0 276
Outro
Valmiki Ramayana Part 93: कैकेयी ने राजा दशरथ से मांगे 2 वरदान ! राम को वनवास और मेरे बेटे भरत को बनाओ राजा
वाल्मीकि रामायण के पिछले लेख में आपने पढ़ा कि, राजा दशरथ ने जब अपनी रानी कैकेयी को कोपभवन में...
Por The Detailing Mafia 2024-02-14 10:41:11 0 1KB