在没有现有文件的情况下使用Mockito / PowerMockito模拟FileInputStream

时间:2016-08-16 19:15:56

标签: java junit mockito powermockito

我正在为使用JUnit的方法编写FileInputStream,而在constructor中只传递文件名。该文件是作为servlet请求的一部分创建的,并且该文件不存储在任何位置。

我正在尝试使用Mock FileInputStream PowerMockito,以便它为我提供了一个模拟文件对象。不幸的是我得到FileNotFoundException这是有效的,但我不知道如何测试这个方法,因为该文件不存在。

待测方法:

public String viewReport() throws Exception {
    this.inputStream = new FileInputStream(DOCUSIGN_REPORT_FILE);

    try {
        boolean returnReport = validateRequest();
        if (returnReport) {
            intgList = this.generateViewIntegrationReportData(getESignUIConfig());
            this.createCSVFile(intgList, new FileWriter(DOCUSIGN_REPORT_FILE));
        } else {
            failureResponse(msgs, 400);
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        msgs.add(new Message(ESignatureIntegrationMessageTypeEnum.MESSAGE_TYPE_ERROR, 
                    UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_CODE_500, UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_TEXT_SERVICE_ERROR));
        failureResponse(msgs, 500);
        return null;
    }

    return UiIntegrationKeyConstants.REPORT_REPSONSE;
} 

到目前为止的JUnit测试。

@Test
public void testViewReport() throws Exception {
    Map<String, Object> actionMap = new HashMap<>();
    actionMap.put("application", "ESignatureIntegrationAction");

    ActionContext.setContext(new ActionContext(actionMap));

    FileInputStream inputStream = Mockito.mock(FileInputStream.class);
    PowerMockito.whenNew(FileInputStream.class).withAnyArguments().thenReturn(inputStream);

    action = new ESignatureIntegrationAction();
    action.viewReport();
}

当代码到达exception

时,我得到new FileInputStream(DOCUSIGN_REPORT_FILE);

感谢您的帮助。

1 个答案:

答案 0 :(得分:-1)

我建议以允许在没有模拟框架的情况下进行测试的方式重构代码。

看起来有点像这样:

page2.html

这样public class YourClass { // ... public String viewReport() { try { boolean isValidRequest = validateRequest(); if (isValidRequest) { IntegrationReportCsvFileHandler fileHandler = new IntegrationReportCsvFileHandler(); IntegrationReportData inputData = fileHandler.readData(new FileInputStream(DOCUSIGN_REPORT_FILE)); IntegrationReportGenerator generator = new IntegrationReportGenerator(); IntegrationReportData outputData = generator.processData(inputData, getESignUIConfig()); fileHandler.writeReport(outputData, new FileWriter(DOCUSIGN_REPORT_FILE)); } else { failureResponse(msgs, 400); return UiIntegrationKeyConstants.FAILURE_RESPONSE; } } catch (Exception e) { e.printStackTrace(); msgs.add(new Message(ESignatureIntegrationMessageTypeEnum.MESSAGE_TYPE_ERROR, UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_CODE_500, UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_TEXT_SERVICE_ERROR)); failureResponse(msgs, 500); return UiIntegrationKeyConstants.FAILURE_RESPONSE; } return UiIntegrationKeyConstants.REPORT_RESPONSE; } // ... } public class IntegrationReportData { // your custom data structure // may as well just be a List<Data> // may be different for input and output } public class IntegrationReportException extends Exception { // your custom exception public IntegrationReportException(String message) { super(exception); } } public class IntegrationReportGenerator { public IntegrationReportData processData(IntegrationReportData data, ESignConfig config) throws IntegrationReportException { // here's your logic that requires testing } } public class IntegrationReportCsvFileHandler { public IntegrationReportData readData(InputStream input) throws IOException { // read data from given input stream } public void writeData(IntegrationReportData data, OutputStreamWriter outputWriter) throws IOException { // write data to given output stream } } 很容易测试。