top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Data Driven Testing using Rest Assured

Rest Assured is a group of Java libraries which enables us to automate Rest API testing. Rest Assured library provides the ability to validate the HTTP responses received from the server. We can verify the status code, status message, headers and even the body of the response. It helps to fetch values of responses from complicated JSON structures. It can be integrated with TestNG and Junit frameworks for testing applications.


For this data driven testing using Rest Assured, I have created a TestNG class, and I am feeding the data from an excel file to my code and performing the test. Also, I have written my script in BDD format which makes it easy to understand.


What is TestNG?


TestNG is an open-source automated testing framework in which NG means Next Generation. It is designed to make end to end testing easy. Using this in our automation testing, helps generate report in a proper format, which shows the number of test cases that have run, passed, failed, and skipped.

For this project, the API I am using to perform the test is as follows


https://lms-admin-rest-service.herokuapp.com/programs


Here, I am posting new programs from an external excel file and testing the Restful web service.


Pre-requisites:


The following should be installed in the system:

  • Java

  • An IDE (I have used eclipse here)

  • Maven


The POM file should have the following dependencies

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --><dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>

This data driven testing can be done in several ways. Given below, are the steps that I have followed, and which worked well for me.


Steps to perform data driven testing using Rest Assured:


1. Create a TestNG class under the respective package in the Maven project and set the base URI and base Path.

2. Create a method to post new programs under @Test annotation passing the parameter from the data provider method.

3. Create a method under @DataProvider annotation to get the data from excel.

4. Run the script and verify that the test has passed or not.


Step 1: I have created a TestNG class (LMS_API.java) in the data driven testing package under the RestAssuredAutomation project shown as below.



package DataDrivenTesting;
import static io.restassured.RestAssured.basePath;
import static io.restassured.RestAssured.baseURI;
import static io.restassured.RestAssured.given;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;public class LMS_API {
{
baseURI = "https://lms-admin-rest-service.herokuapp.com";
basePath = "/programs";
}

LMS_API.java file


Step 2: The method post_programs shown below creates new programs by receiving the data from the data provider method. Here, it also checks for the expected status code, and programs are created by using assertions.

@Test(dataProvider = "progdata")
public void post_programs(String progdesc, String progname) 
{
//Creating Json object to send data along with post request
JSONObject requestparams = new JSONObject();
// requestparams.put("online", "true");
requestparams.put("programDescription", progdesc);
requestparams.put("programName", progname);
// requestparams.put("programId", "601");
Response resp_prog_details = given().auth().basic("Admin", "password").header("Content-Type", "application/json").body(requestparams.toJSONString()).when().post().then().assertThat().statusCode(200).log().all().extract().response();
//Asserting the status code is success
int statusCode = resp_prog_details.getStatusCode();
Assert.assertEquals(statusCode, 200);
System.out.println("The response code is "+statusCode);
String responseBody = resp_prog_details.getBody().asPrettyString();
//Asserting the correct values are posted
Assert.assertEquals(responseBody.contains(progdesc), true);
Assert.assertEquals(responseBody.contains(progname), true);
}

Step 3: The method get_prog_data gets data from the excel file and sends to the method which calls it.

@DataProvider(name = "progdata")
String[][] get_prog_data() throws IOException {
String path = System.getProperty("user.dir") + "/src/test/java/DataDrivenTesting/ProgData.xlsx";
int rownum = XLUtils.getRowCount(path, "Sheet1");
int colnum = XLUtils.getCellCount(path, "Sheet1", 1);
String progdata[][] = new String[rownum][colnum];for (int i = 1; i <= rownum; i++) 
{
for (int j = 0; j < colnum; j++) 
{
progdata[i - 1][j] = XLUtils.getCellData(path, "Sheet1", i, j);
}
}
return progdata;
}

Data in the excel file


Please note I have stored all the reusable methods which can be used to read and write data from the excel file in a separate class (XLUtils.java) in the same package. The ProgData.xlsx file is also saved in the same package. Below is the code stored in XLUtils.java file.

package DataDrivenTesting;import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class XLUtils {
public static FileInputStream fi;
public static FileOutputStream fo;
public static XSSFWorkbook wb;
public static XSSFSheet ws;
public static XSSFRow row;
public static XSSFCell cell;public static int getRowCount(String xlFile, String xlSheet) throws IOException {
fi = new FileInputStream(xlFile);
wb = new XSSFWorkbook(fi);
ws = wb.getSheet(xlSheet);
int rowCount = ws.getLastRowNum();
wb.close();
fi.close();
return rowCount;
}public static int getCellCount(String xlFile, String xlSheet, int rowNum) throws IOException{
fi = new FileInputStream(xlFile);
wb = new XSSFWorkbook(fi);
ws = wb.getSheet(xlSheet);
row = ws.getRow(rowNum);
int columnCount = row.getLastCellNum();
wb.close();
fi.close();
return columnCount;
}public static String getCellData(String xlFile, String xlSheet, int rowNum, int columnNum) throws IOException{
fi = new FileInputStream(xlFile);
wb = new XSSFWorkbook(fi);
ws = wb.getSheet(xlSheet);
row = ws.getRow(rowNum);
cell = row.getCell(columnNum);
String data;try {
DataFormatter formatter = new DataFormatter();
data = formatter.formatCellValue(cell);
} catch (Exception e) {
data = "";
}
wb.close();
fi.close();
return data;
}public static void setCellData(String xlFile, String xlSheet, int rowNum, int columnNum, String data) throws IOException {
fi = new FileInputStream(xlFile);
wb = new XSSFWorkbook(fi);
ws = wb.getSheet(xlSheet);
row = ws.getRow(rowNum);
cell = row.createCell(columnNum);
cell.setCellValue(data);
fo = new FileOutputStream(xlFile);
wb.write(fo);
wb.close();
fo.close();
fi.close();
}
}

Step 4: Run the script and verify if the tests have passed or not.


Output of the test

Some additional information which might be interesting to know:

  • Rest Assured uses Hamcrest Matchers for comparing actual response with the expected response.

  • Apache POI is a popular API that allows programmers to create, modify, and display MS Office files using Java programs.

  • XSSF (XML Spreadsheet Format) is a component of Apache POI, and it is used for xlsx file format of MS-Excel.

Hope this blog was useful !!!!

435 views0 comments

Recent Posts

See All
bottom of page