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/

Rechercher
Commandité
Catégories
Lire la suite
Autre
Call Girl Service in Jaipur | Sara Call Girl
You will surely experience the full complements of truly charming Call Girl Service in Jaipur if...
Par Sara Sharma 2022-04-18 11:21:47 0 3KB
Industry
Benefits Of Pharmacy Automation In Improving Medical Care
You need to be precise when managing large patient data and attending to each person's medical...
Par Narola Infotech 2023-11-28 09:30:55 0 908
Autre
https://ask24x7.com/igenics-reviews-benefits-side-effects-price-us/
iGenics is a natural vision supplement that works quickly and contains some of the most potent...
Par Health Menia 2022-04-23 16:20:14 0 2KB
Autre
Where to Buy SEO Services
Everyone who requires SEO can definitely benefit from the sale of SEO. why not Since you offer...
Par Hunain Khatri 2022-12-12 12:10:12 0 2KB
Autre
Metering Pump Market Size, Share | Industry Report 2024-32
IMARC Group's report titled "Metering Pump Market Report by Type (Diaphragm Pumps, Piston Pumps,...
Par Rocky Rai 2024-04-18 05:37:36 0 489