如何在java中测试从excel文件中读取数据的方法?

xxhby3vn  于 2023-11-15  发布在  Java
关注(0)|答案(2)|浏览(86)

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()编写测试用例,或者如何测试列的每个字段是否包含数据?

fdbelqdn

fdbelqdn1#

  • 所以,你想测试readRow()方法,它将返回FacebookFields。
  • 因为它是私有的,所以你不能直接对这个私有方法进行单元测试,但是你的主要目标似乎是测试这个方法。
  • 你可以创建一个类FacebookFieldRetriever,它有一个公共方法retrieveFromRow(),它返回FacebookFields
  • 因此,每次FacebookDataExtraction遇到excel工作表中的一行时,它都会创建一个FacebookFieldRetriever对象,并调用retrieveFromRow()来获取从该行建模的FacebookFields对象。
  • 既然我们已经通过了类设计阶段,让我们继续进行单元测试。所以,现在你有了一个公共方法:retrieveFromRow(),你可以对它进行单元测试,假设你有一个行对象,它包含的单元格包含Excel工作表中的实际值。(基本上,我们关注的是测试开关逻辑)。
  • FacebookFieldRetriever类

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 {

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);
}
}

字符串
`

  • 你可以用上面的方法来测试。希望对你有帮助。
dced5bon

dced5bon2#

如果你有一个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);
}

字符串

相关问题