繁体   English   中英

如何使用模拟来测试Java Junit中带有参数的方法?

[英]How to use mocking to test the methods with parameters in java Junit?

我有一个类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.readFromExcelFile();
        System.out.println("The Size of the list is:"+ displayList.size());
        //System.out.println(displayList);
    }

    public List<FacebookFields> readFromExcelFile() {
        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;
    }

    public 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; 
            default:System.out.println("Missing record at row :" + row.getRowNum() + " column :" +  cell.getColumnIndex() );

            }
        }

        return record;
    }

    public boolean containsData() {  

        List<FacebookFields> checkList= readFromExcelFile();    
        return !checkList.isEmpty() ;
    }

}

我已经编写了测试用例,以检查在调用readFromExcelFile()方法之后列表即fbList包含数据。

@Test
    public void testWhetherListConatinsData(){
        FacebookDataExtraction fbDataList= new FacebookDataExtraction();
        assertEquals(fbDataList.containsData(), true);  
    }

我建议,该方法readSheet()可以用嘲讽的参数进行测试Sheet sheet如何测试方法readSheet()我是新来的嘲讽,任何人都可以解释它如何为方法来完成readSheet()与测试用例对于iffor循环两种。

您可以创建存根Sheet 也就是说,创建一个扩展Sheet并覆盖您要影响的方法的类。

在将readSheet()标记为public访问器后,您可以调用: readSheet(<instance of your extended Sheet class>, sheetIndex, fbList)

或者,如果您想简化此任务,则可以使用Mockito

但是,我认为readSheet如果返回了某些内容,而不是从其调用者访问列表,则它更适合于单元测试,否则您可以在测试中初始化List<FacebookFields> ,并将其传递给readSheet()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM