Payload extraction from excel sheets
How It Works
A Java library such as Apache POI reads data from an Excel file cell by cell, which the automation code then assembles into a JSON (or other) request payload before sending it via Rest Assured.
Example Flow
Workbook wb = WorkbookFactory.create(new FileInputStream("data.xlsx"));
Sheet sheet = wb.getSheet("Users");
String name = sheet.getRow(1).getCell(0).getStringCellValue();
String job = sheet.getRow(1).getCell(1).getStringCellValue();
given().contentType("application/json")
.body("{ \"name\":\"" + name + "\", \"job\":\"" + job + "\" }")
.when().post("/users")
.then().statusCode(201);
Why This Matters
This is the core mechanism that powers a payload-driven framework, letting one test method run repeatedly with many different data combinations pulled straight from the spreadsheet.
Setting Up Apache POI
The poi-ooxml artifact reads modern .xlsx files and pulls in the core POI library automatically. Rest Assured also needs a JSON library such as Jackson on the classpath if you want it to turn Java maps into JSON bodies.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.1</version>
</dependency>
Keep the workbook under src/test/resources so it travels with the project and paths work on every machine and CI agent.
Reading Every Row Into Maps
Reading single cells by number becomes painful as sheets grow. A better approach treats the first row as headers and returns one map per data row:
public static List<Map<String, String>> readRows(String file, String sheetName) throws IOException {
List<Map<String, String>> rows = new ArrayList<>();
DataFormatter fmt = new DataFormatter();
try (InputStream in = new FileInputStream(file);
Workbook wb = WorkbookFactory.create(in)) {
Sheet sheet = wb.getSheet(sheetName);
Row header = sheet.getRow(0);
for (int r = 1; r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r);
if (row == null) continue; // skip blank rows
Map<String, String> data = new LinkedHashMap<>();
for (int c = 0; c < header.getLastCellNum(); c++) {
String key = fmt.formatCellValue(header.getCell(c));
String value = fmt.formatCellValue(row.getCell(c));
data.put(key, value);
}
rows.add(data);
}
}
return rows;
}
DataFormatter returns what the cell displays, so a number like 25 comes back as "25" instead of throwing an error the way getStringCellValue() does on numeric cells. It also returns an empty string for a missing cell rather than causing a NullPointerException.
Worked Example: Sheet Rows Become Requests
Imagine a sheet named Users with columns name, job and expectedStatus. A TestNG data provider feeds each row to the test, and the body is built from a map instead of a hand-joined string, so quotes and special characters are escaped correctly:
@DataProvider(name = "users")
public Object[][] users() throws IOException {
List<Map<String, String>> rows =
ExcelReader.readRows("src/test/resources/data.xlsx", "Users");
Object[][] data = new Object[rows.size()][1];
for (int i = 0; i < rows.size(); i++) data[i][0] = rows.get(i);
return data;
}
@Test(dataProvider = "users")
public void createUser(Map<String, String> row) {
Map<String, Object> body = new HashMap<>();
body.put("name", row.get("name"));
body.put("job", row.get("job"));
given().contentType(ContentType.JSON).body(body)
.when().post("/users")
.then().statusCode(Integer.parseInt(row.get("expectedStatus")));
}
A row with an empty name and an expected status of 400 now tests a negative case without any new code. Testers who know nothing about Java can add rows to the sheet, while the automation engineer only maintains the reader and the test method.
Problems You Will Meet and How to Handle Them
- Numbers shown as "25.0". This happens with
getNumericCellValue();DataFormatteravoids it. - Trailing empty rows. Rows that once had formatting still count in
getLastRowNum(); skip rows whose key cells are blank. - File locked on Windows. Close the workbook in Excel before running tests, and always close streams with try-with-resources.
- Header typos. A column called "Name " with a space will not match
row.get("name"); trim keys when reading. - Types in JSON. Everything read is text; convert values like age or price with
Integer.parseIntbefore putting them in the body if the API expects numbers. - Sensitive data. Keep real passwords or personal details out of committed spreadsheets; use test accounts.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.