类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()
编写测试用例或如何测试列的每个字段是否包含数据?
答案 0 :(得分:0)
retrieveFromRow()
,其中包含一个公共方法FacebookFields
,该方法返回FacebookDataExtraction
。FacebookFieldRetriever
遇到excel表格中的一行时,它都会创建一个retrieveFromRow()
的对象,并调用FacebookFields
以从该行建模retrieveFromRow()
对象。< / LI>
当我们通过课程设计阶段时,让我们继续进行单元测试。所以,现在你有一个公共方法:class FacebookFieldRetriever{
你可以实际单元测试,假设你有一个行对象,其中包含包含excel表中实际值的单元格。 (基本上,我们专注于测试开关案例逻辑)。
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;
}
}
FacebookFieldRetriever Class
private final String TEST_NAME = "Mark Zuckerberg";
private final String TEST_id = "4";
private final String TEST_DATE = "2015-10-13;
private final String TEST_MSG = "Welcome to Facebook";
private final String TEST_TYPE = "SomeType";
private final String TEST_PAGE = "SomePage";
private final String TEST_LIKECOUNT = 6;
private final String TEST_COMMENTCOUNT = 7;
private final String TEST_SHARECOUNT = 8 ;
public void testFacebookFieldRetriever()
{
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheettemp = wb.createSheet();
Row row = sheettemp.createRow(1);
Cell cell = row.createCell(0);
cell.setCellValue(TEST_NAME);
Cell cell1 = row.createCell(1);
cell.setCellValue(TEST_id);
Cell cell2 = row.createCell(2);
cell.setCellValue(TEST_DATE);
Cell cell3 = row.createCell(3);
cell.setCellValue(TEST_MSG);
Cell cell4 = row.createCell(4);
cell.setCellValue(TEST_TYPE);
Cell cell5 = row.createCell(5);
cell.setCellValue(TEST_PAGE);
Cell cell6 = row.createCell(6);
cell.setCellValue(TEST_LIKECOUNT);
Cell cell7 = row.createCell(7);
cell.setCellValue(TEST_COMMENTCOUNT);
Cell cell8 = row.createCell(8);
cell.setCellValue(TEST_SHARECOUNT);
FacebookFieldRetriever fbRetriever = new FacebookFieldRetriever();
FacebookFields fbFields = fbRetriever.retrieveFromRow(row);
assertEquals(fbFields.getName(), TEST_NAME);
assertEquals(fbFields.getId(), TEST_id);
assertEquals(fbFields.getMessage(), TEST_MSG);
assertEquals(fbFields.getLikeCount(), TEST_LIKECOUNT);
assertEquals(fbFields.getShareCount(), TEST_SHARECOUNT);
assertEquals(fbFields.getCommentCount(), TEST_COMMENTCOUNT);
assertEquals(fbFields.getPage(), TEST_PAGE);
assertEquals(fbFields.getType(), TEST_TYPE);
}
}
单元测试示例下面:
类FacebookFieldRetrieverTest {
{{1}}
`
答案 1 :(得分:0)
如果您的excel包含示例excel中第一行的“期望值”,则以下测试可能是一个开始。请注意,列表的equals
可能有点粗糙,特别是因为我不确定它如何用心对待元素的内部顺序。
编辑我在FacebookDataExtraction
类中添加了一个构造函数参数,该参数将文件名带到应该从中提取数据的excel文件。
@Test
public void testWhetherListConatinsData(){
List<FacebookFields> expectedList = new ArrayList<>();
// Fill the expected list here:
expectedList.add("expected value");
// ... Add more if you want to...
// Read in data.
FacebookDataExtraction fbDataList= new FacebookDataExtraction("path/to/testdata/test_with_strange_first_name.xlsx");
List<FacebookFields> listOfFields = fbDataList.readFromExcel();
// Good check to start with.
assertEquals(listOfFields.containsData(), true);
// Actual data check.
assertEquals("Lists not the same", expectedList, listOfFields);
}