使用JUnit进行Struts2操作

时间:2016-08-02 15:56:18

标签: java junit struts2

我已使用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

0 个答案:

没有答案