To set up this Rest Assured -TestNG framework we will use POIJI to read data from Excel files instead of POI.
What is POIJI API:-
Poiji is a powerful Java library/API that simplifies the process of reading Excel files in Java applications.
It programmatically maps excel sheet to Java classes and converts each row of the excel data into Java object.
This is a API created on top of the most popular Apache POI. It is available in the maven repository.
It uses lots of annotations to make our tasks even easier like Annotations: @ExcelCellName, @ExcelCell, @ExcelRow.
Read Excel data in Java using Poiji API includes 3 simple steps as shown below:
Step 1: Include the Poiji Dependency
We will add following ‘poiji’ dependency in our maven project's pom.xml to get the features of Microsoft Excel.
<dependency>
<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>4.1.1</version>
</dependency>
Step 2: Create Java POJOs (Plain Old Java Object) and Annotate Java Fields
Create Java classes (POJOs) that correspond to the structure of the data we intend to extract from the Excel file.
In theses classes, we will use Poiji to map Excel columns name to Java object fields using Poiji annotation "@ExcelCellName" which specify the column mapping, date formats.
What is POJO class:-
A POJO class in Java generally consists of variable values, getters, and setters.
POJOs are free objects that help conduct data transfer, connect the components of a distributed operation model, improve communication between the layers of an application, and integrate variables in Java or encapsulation.
Step 3:Read Excel Data
Use Poiji to read the Excel data and convert it into Java objects. Poiji provides method name "fromExcel" to read data from Excel files and return it as a list of Java objects.
Creating Rest Assured framework using POIJI:
Step 1: Set Up Maven Project and add framework structure.
Create a new Maven project and add the necessary dependencies in your pom.xml file.
rest-assured
testng
poiji
json
json-path
json-schema-validator
jackson-core
lombok
poi
poi-ooxml
maven-surefire-plugin
maven-compiler-plugin
Framework structure:-
Step 2: Create an Excel file with test data and add that sheet in project under resources/TestData folder.
For example, POST_PUT_UserAPI_RestAssu.xlsx with the following content:
Step 3: Create POJOs for Excel Data
Create separate POJO classes to map the Excel data using POIJI annotations, for Address and User Request and Response .
Step 4: Create a Utility Class to Read Excel Data
Use POIJI to read the Excel data into a list of TestData objects.
Step 5: Create a base class to set up endpoint URLs of user api.
Step 6: Create a class to set up all CURD operations of user api.
This is the class which will have implementation for all the CURD operation for user module.
In case more than 1 module in project ,Then we need to create separate classes for CURD methods for each module.
Step 7: Create Test Classes Using TestNG for each request type like for POST ,Delete, get etc.
These TestNG test classes to execute the tests using data from the Test data Excel file.
In these classes we will use "ITestContext " to store and share data across the tests by using TestNG framework.
This will help chaining all request to perform end to end API testing and to do so we need to set and get ITestContext value in variables.
Step 8: Create a TestNG XML suite to run the tests via TestNG XML.
Step 9: Run the Tests from terminal.
Execute the tests by running the testng.xml suite file using the Maven Surefire Plugin from the terminal:
first update pom.xml with testng.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
And execute following cmd:
mvn test -Dsurefire.suiteXmlFiles=testng.xml
This execution will allow you to perform API tests using Rest-Assured and TestNG, with test data being read from an Excel file using POIJI.
Hopefully this will help to reduce the complexity for excel reading part in any testing framework.
****** HAPPY LEARNING******