Test Overview
This test reads data from an Excel file using Apache POI and verifies that the expected value is present in a specific cell.
This test reads data from an Excel file using Apache POI and verifies that the expected value is present in a specific cell.
import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.jupiter.api.*; import java.io.FileInputStream; import java.io.IOException; import static org.junit.jupiter.api.Assertions.*; public class ExcelDataReadingTest { @Test public void testReadExcelCell() throws IOException { String filePath = "src/test/resources/testdata.xlsx"; FileInputStream fis = new FileInputStream(filePath); Workbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(1); // second row (index 1) Cell cell = row.getCell(0); // first column (index 0) String cellValue = cell.getStringCellValue(); workbook.close(); fis.close(); assertEquals("TestUser", cellValue, "Excel cell value should be 'TestUser'"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | - | PASS |
| 2 | Opens Excel file 'src/test/resources/testdata.xlsx' using FileInputStream | Excel file stream opened successfully | - | PASS |
| 3 | Creates XSSFWorkbook object from file stream | Workbook loaded with sheets and data | - | PASS |
| 4 | Accesses first sheet (index 0) | Sheet object representing first sheet ready | - | PASS |
| 5 | Reads second row (index 1) and first cell (index 0) | Cell object with string value retrieved | - | PASS |
| 6 | Extracts string value from cell | String value 'TestUser' obtained | - | PASS |
| 7 | Closes workbook and file input stream | Resources released | - | PASS |
| 8 | Asserts that cell value equals 'TestUser' | Assertion compares expected and actual values | assertEquals("TestUser", cellValue) | PASS |