Building an API testing framework using RestAssured and TestNG with data-driven testing
Below are the detailed outline and sample code for building an API testing framework using RestAssured and TestNG with data-driven testing-
Project Structure
`
api-testing-framework/
│
├── src/main/java/
│ └── utils/
│ └── ExcelUtils.java
│
├── src/test/java/
│ ├── base/
│ │ └── BaseTest.java
│ │
│ ├── testcases/
│ │ └── ApiTest.java
│ │
│ ├── testdata/
│ │ └── TestData.xlsx
│ │
│ ├── resources/
│ │ └── config.properties
│ │
│ └── testng.xml
│
├── pom.xml
└── README.md
`
Step 1: Add Dependencies in `pom.xml`
Ensure your `pom.xml` has the necessary dependencies for RestAssured, TestNG, and Apache POI (for Excel data handling).
`xml
<dependencies>
<!-- RestAssured Dependency -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.3.0</version>
</dependency>
<!-- TestNG Dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version>
<scope>test</scope>
</dependency>
<!-- Apache POI for Excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
</dependencies>
`
Step 2: Create `BaseTest.java`
This class will set up the basic configuration for RestAssured and handle common setup or teardown tasks.
`java
package base;
import io.restassured.RestAssured;
import org.testng.annotations.BeforeClass;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class BaseTest {
protected Properties config = new Properties();
@BeforeClass
public void setUp() {
loadConfig();
RestAssured.baseURI = config.getProperty("baseURI");
}
private void loadConfig() {
try (FileInputStream fis = new FileInputStream("src/test/resources/config.properties")) {
config.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
}
}
`
Step 3: Create `ExcelUtils.java`
This utility class will handle reading data from Excel files.
`java
package utils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtils {
public static List<Object[]> getTestData(String sheetName) {
List<Object[]> data = new ArrayList<>();
try (FileInputStream fis = new FileInputStream("src/test/testdata/TestData.xlsx");
Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheet(sheetName);
int rows = sheet.getPhysicalNumberOfRows();
for (int i = 1; i < rows; i++) {
Row row = sheet.getRow(i);
int cols = row.getPhysicalNumberOfCells();
Object[] rowData = new Object[cols];
for (int j = 0; j < cols; j++) {
rowData[j] = getCellValue(row.getCell(j));
}
data.add(rowData);
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
private static Object getCellValue(Cell cell) {
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
return cell.getNumericCellValue();
case BOOLEAN:
return cell.getBooleanCellValue();
default:
return null;
}
}
}
`
Step 4: Create `ApiTest.java`
This class will contain the actual test cases. It uses data from the Excel sheet to drive the tests.
`java
package testcases;
import base.BaseTest;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.ExcelUtils;
import java.util.List;
import static io.restassured.RestAssured.given;
public class ApiTest extends BaseTest {
@DataProvider(name = "apiData")
public Object[][] getApiData() {
List<Object[]> testData = ExcelUtils.getTestData("Sheet1");
return testData.toArray(new Object[0][0]);
}
@Test(dataProvider = "apiData")
public void testApi(String endpoint, String method, String requestBody, double expectedStatusCode) {
RequestSpecification request = given();
if (!requestBody.isEmpty()) {
request.body(requestBody);
}
Response response;
switch (method.toUpperCase()) {
case "POST":
response = request.post(endpoint);
break;
case "PUT":
response = request.put(endpoint);
break;
case "DELETE":
response = request.delete(endpoint);
break;
default:
response = request.get(endpoint);
break;
}
Assert.assertEquals(response.getStatusCode(), (int) expectedStatusCode);
}
}
`
Step 5: Create `testng.xml`
This configuration file will define how the tests should be run.
`xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="API Test Suite">
<test name="API Tests">
<classes>
<class name="testcases.ApiTest"/>
</classes>
</test>
</suite>
`
Step 6: Create `config.properties`
This file will store the base URI and other configuration details.
`
baseURI=https://api.example.com
`
Step 7: Sample Excel Data
Create an Excel file (`TestData.xlsx`) with a sheet named `Sheet1` to store your test data.
| Endpoint | Method | RequestBody | ExpectedStatusCode |
|------------------|--------|------------------------|--------------------|
| /users | GET | | 200 |
| /users/1 | GET | | 200 |
| /users | POST | {"name":"John Doe"} | 201 |
| /users/1 | PUT | {"name":"Jane Doe"} | 200 |
| /users/1 | DELETE | | 204 |
Step 8: Running the Tests
You can run the tests using TestNG from your IDE or from the command line.
Comments
Post a Comment