you can use TestNG annotation @dataprovider (It will keep running your @test with different set of Test-Data).
Please find the below code snippet:
@DataProvider(name="empLogin")
public Object[][] loginData() {
Object[][] arrayObject = getExcelData("D:/sampledoc.xls","Sheet1");
return arrayObject;
}
Utility to read the data from excel Using Apache poi:-
public String[][] getExcelData (String excelPath, String sheetName)
{
String[][] arrayExcelData = null;
try
{
fis=new FileInputStream(excelPath);
workbook=new XSSFWorkbook(fis);
sheet=workbook.getSheet(sheetName);
int totalNoOfCols = sheet.getRow(1).getLastCellNum();
int totalNoOfRows = sheet.getLastRowNum();
arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];
for (int i= 1 ; i < totalNoOfRows; i++) {
for (int j=0; j < totalNoOfCols; j++) {
arrayExcelData[i-1][j] =sheet.getCell(j,i).getContents();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}
return arrayExcelData;
}