【发布时间】:2015-10-14 11:18:30
【问题描述】:
我有一个 FacebookDataExtraction 类,它从 Excel 文件中读取数据并将数据存储为行对象列表,如代码所示。
我已经使用 config.properties 文件来获取文件路径。 config.properties 文件内容为:
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() ;
}
}
我已经编写了测试用例来检查列表(即fbList)在调用readFromExcelFile() 方法后是否包含数据。
@Test
public void testWhetherListConatinsData(){
FacebookDataExtraction fbDataList= new FacebookDataExtraction();
assertEquals(fbDataList.containsData(), true);
}
我得到建议可以通过模拟参数Sheet sheet 来测试方法readSheet()
如何测试方法readSheet()
我是嘲笑的新手,谁能解释如何使用if 和for 循环的测试用例readSheet() 来完成。
【问题讨论】:
标签: java unit-testing mocking mockito junit4