x1c 0d1x上图显示了包含Facebook数据的Excel文件。
类FacebookDataExtraction
从Excel文件中读取数据,并将数据存储为行对象列表,如代码所示。我利用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()
编写测试用例,或者如何测试列的每个字段是否包含数据?
2条答案
按热度按时间fdbelqdn1#
FacebookFieldRetriever
,它有一个公共方法retrieveFromRow()
,它返回FacebookFields
。FacebookDataExtraction
遇到excel工作表中的一行时,它都会创建一个FacebookFieldRetriever
对象,并调用retrieveFromRow()
来获取从该行建模的FacebookFields
对象。retrieveFromRow()
,你可以对它进行单元测试,假设你有一个行对象,它包含的单元格包含Excel工作表中的实际值。(基本上,我们关注的是测试开关逻辑)。class FacebookFieldRetriever{ public FacebookFields retrieveFromRow(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; } }
class FacebookFieldRetrieverTest {
字符串
`
dced5bon2#
如果你有一个excel,在你的示例excel中,第一行包含“期望值”,下面的测试可以作为一个开始。注意,列表的
equals
可能有点粗糙,特别是因为我不确定它是如何处理元素的内部顺序的。FacebookDataExtraction
类添加了一个构造函数参数,它将文件名带到它应该从中提取数据的excel文件。字符串