2016-08-16 537 views
-1

我正在为使用FileInputStream的方法编写JUnit,并且在constructor中只传递文件名。该文件是作为servlet请求的一部分创建的,并且此文件不存储在任何位置。在没有现有文件的情况下使用Mockito/PowerMockito模拟FileInputStream

我想MockFileInputStream使用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);

感谢您的帮助。

+2

通常的警告:如果你要使用工厂(和依赖注入一起)......那么你不需要Powermock来模拟一个新的调用。 – GhostCat

+0

你的意思是我应该使用Spring注入FileInputStream? – Jaykumar

+0

如果您的代码无法从中读取某些文件,您的测试会测试什么? –

回答

0

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

它可能看起来有点像这样:

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 
    } 

} 

这样的IntegrationReportGenerator会容易测试。

相关问题