上圖顯示了包含Facebook數據的excel文件。如何測試從java中的excel文件中讀取數據的方法?
類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',這意味着它是一個內部方法。你應該嘗試着重於測試班級的「公共」方法。如果您設計了一個/多個公開方法的良好測試,您將自動測試私有方法是否奏效。 – Timo
@dom我想要的單元測試。 – ShridharBavannawar