Reading test data from an Excel file using JUnit and Apache POI
To maintain test cases and test data, Microsoft Excel is the favorite tool used by testers. Compared to the CSV file format, Excel gives numerous features and a structured way to store data. A tester can create and maintain tables of test data in an Excel spreadsheet easily.
In this recipe, we will use an Excel spreadsheet as your data source. We will use the Apache POI API, developed by the Apache Foundation, to manipulate the Excel spreadsheet. This recipe also implements some negative test handling.
Getting ready
Set up a new project and add JUNIT4 to project's build path
Download and add the following Apache POI JAR files to the project's build path, from http://poi.apache.org/:
poi-3.8.jar
poi-ooxml-3.8.jar
poi-ooxml-schemas-3.8.jar
dom4j-1.6.1.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
Create an Excel spreadsheet with the required data
We will also need a SpreadsheetData
helper class to read Excel spreadsheets. This is...