【发布时间】:2016-01-11 06:43:44
【问题描述】:
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.readFromExcel();
System.out.println("The Size of the list is:"+ displayList.size());
}
public List<FacebookFields> readFromExcel() {
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;
}
private 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;
}
}
return record;
}
public boolean containsData() {
List<FacebookFields> checkList= readFromExcel();
return !checkList.isEmpty() ;
}
}
FacebookFields 类(此处未显示)包含提取数据的设置方法!
如何编写readRow()方法的测试用例或如何测试列的每个字段是否包含数据?
【问题讨论】:
-
单元测试?集成测试?你希望你的测试带来什么价值?
-
你做了
private的方法,也就是说它是一个内部方法。您应该尝试专注于测试该类的public方法。如果您为公共方法设计了一个/多个良好的测试,您将自动测试私有方法是否有效。 -
@dom 我想要它的单元测试。
-
@Timo 是的私有方法无法测试。是否公开了我如何测试它?只是我想检查每列中的数据是否不为空。我该怎么做?
-
将这个类拆分成许多只做一件事的小类(单一职责原则),然后通过构造函数注入你的依赖项,用所有小类构建你的主方法(依赖倒置原则) )。每个小类都应该可以在不了解其他类的情况下进行测试,并且会逐步建立。
标签: java junit4 properties-file testcase