【问题标题】:How to use mocking to test the methods with parameters in java Junit?java - 如何在java Junit中使用模拟来测试带参数的方法?
【发布时间】:2015-10-14 11:18:30
【问题描述】:

我有一个 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() ;
    }

}

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

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

我得到建议可以通过模拟参数Sheet sheet 来测试方法readSheet() 如何测试方法readSheet() 我是嘲笑的新手,谁能解释如何使用iffor 循环的测试用例readSheet() 来完成。

【问题讨论】:

    标签: java unit-testing mocking mockito junit4


    【解决方案1】:

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

    由于您将readSheet() 标记为public 访问器,因此您只需调用:readSheet(&lt;instance of your extended Sheet class&gt;, sheetIndex, fbList)

    如果你想让这个任务更简单,你可以使用Mockito

    但是,我认为readSheet 会更适合单元测试,如果它返回一些东西,而不是从其调用者访问列表,否则您可以在测试中初始化一个List&lt;FacebookFields&gt;,并将其传递给@987654329 @。

    【讨论】:

    • 我不知道该怎么做。
    • 老实说,我认为您的课程需要更改或重组以支持单元测试。例如readSheet() 依赖于全局变量workbook,它只在readFromExcelFile() 中初始化。 readSheet() 依赖自身范围之外的变量会使单元测试变得困难和混乱。
    • 可以在 readFromExcelFile() 中初始化工作簿。如何在我的测试用例中初始化参数 List fbList
    • 您的readSheet() 方法依赖于readFromExcelFile() 为您初始化某些东西。你可以在你的单元测试设置中调用readFromExcelFile(),但是当你最终从文件系统中读取时,这违背了模拟工作表的目的。我建议阅读单元测试以及如何使方法更加独立。
    • 对修改代码有什么建议吗?我在想的是方法 readSheet() 将返回行对象列表,以便在方法中定义和初始化 workbook。其余的一切都保持不变。我不知道它是否解决了问题,因为我们将留下要测试的方法readRow
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    相关资源
    最近更新 更多