我已使用Struts2
创建了一个操作方法,该方法返回stream
以下载文件。我没有在任何地方保存文件。在使用JUnit
测试此操作时,我得到FileNotFound
这是有效的,因为该文件未保存在任何位置,因为当我调用该操作时,它将变为stream
。那么,我该如何测试这种情况呢?
行动方法。
private static final String DOCUSIGN_REPORT_FILE = "DocuSignEnvelopeReport.csv";
public String viewReport() throws Exception {
boolean returnReport;
inputStream = new FileInputStream(DOCUSIGN_REPORT_FILE);
try {
returnReport = validateRequest();
if (returnReport) {
intgList = this.generateViewIntegrationReportData(getESignUIConfig());
this.createCSVFile(intgList, 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;
}
struts.xml中
<action name="*Integration" method="{1}" class="com.mercuryinsurance.esignature.integration.webapp.action.ESignatureIntegrationAction">
<result name="success" type="tiles">integrationView</result>
<result name="reportResponse" type="stream">
<param name="contentType">application/text/csv</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="DocuSignEnvelopeReport.csv"</param>
<param name="bufferSize">4096</param>
</result>
</action>
JUnit到目前为止
@Test
public void testViewReport() throws Exception {
Map<String, Object> actionMap = new HashMap<>();
actionMap.put( "application", "ESignatureIntegrationAction" );
ActionContext context = new ActionContext( actionMap );
ActionContext.setContext(context);
action = new ESignatureIntegrationAction();
InputStream inputStream = new FileInputStream( new File( "DocuSignEnvelopeReport.csv" ) );
action.setInputStream( inputStream );
String actionReturn = action.viewReport();
assertNotNull( actionReturn );
}
当它到达inputStream = new FileInputStream(DOCUSIGN_REPORT_FILE);
时,它会抛出FileNotFound
。