I have a class FacebookDataExtraction
that reads the data from the Excel file and stores the data as list of row objects as shown in the code.
I have made use of the config.properties file to get the file path. The contents of config.properties file are:
FILE_NAME=D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx.
public class FacebookDataExtraction {
//private static final String FILE_NAME="D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx";
private static final String SHEET_NAME="nextv54plus_actions";
XSSFWorkbook workbook;
public static void main(String[] args){
FacebookDataExtraction obj= new FacebookDataExtraction();
List<FacebookFields> displayList= new ArrayList<FacebookFields>();
displayList=obj.readFromExcelFile();
System.out.println("The Size of the list is:"+ displayList.size());
//System.out.println(displayList);
}
public List<FacebookFields> readFromExcelFile() {
List<FacebookFields> fbList= new ArrayList<FacebookFields>();
try
{
ReadPropertyFile data= new ReadPropertyFile("config.properties");
FileInputStream fin= new FileInputStream(data.getPropertyFor("FILE_NAME"));
workbook= new XSSFWorkbook(fin);
int sheetIndex=0;
for (Sheet sheet : workbook) {
readSheet(sheet,sheetIndex ++, fbList);}
}catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
return fbList;
}
public void readSheet(Sheet sheet, int sheetIndex , List<FacebookFields> fbList) {
if(SHEET_NAME.equals(sheet.getSheetName())){
workbook.removeSheetAt(sheetIndex);
return;
}
for (Row row : sheet){
if (row.getRowNum() > 0)
fbList.add(readRow(row));}
}
private FacebookFields readRow(Row row) {
FacebookFields record= new FacebookFields();
for (Cell cell : row) {
switch (cell.getColumnIndex()) {
case 0: record.setName(cell.getStringCellValue());
break;
case 1: record.setId(cell.getStringCellValue());
break;
case 2: record.setDate(cell.getStringCellValue());
break;
case 3: record.setMessage(cell.getStringCellValue());
break;
case 4: record.setType(cell.getStringCellValue());
break;
case 5: record.setPage(cell.getStringCellValue());
break;
case 6: record.setLikeCount(String.valueOf(cell.getNumericCellValue()));
break;
case 7: record.setCommentCount(String.valueOf(cell.getNumericCellValue()));
break;
case 8: record.setShareCount(String.valueOf(cell.getNumericCellValue()));
break;
default:System.out.println("Missing record at row :" + row.getRowNum() + " column :" + cell.getColumnIndex() );
}
}
return record;
}
public boolean containsData() {
List<FacebookFields> checkList= readFromExcelFile();
return !checkList.isEmpty() ;
}
}
I have written test case to check if the list i.e fbList
contains data after the method readFromExcelFile()
is called.
@Test
public void testWhetherListConatinsData(){
FacebookDataExtraction fbDataList= new FacebookDataExtraction();
assertEquals(fbDataList.containsData(), true);
}
I got suggestion that the method readSheet()
can be tested by mocking the parameter Sheet sheet
How do I test the method readSheet()
I'm new to mocking, can anyone explain how it can be done for the method readSheet()
with testcases for if
and for
loop both.