如何使用ArgumentCaptor验证写入HttpServletResponse的字节
发布时间:2020-12-15 04:42:05 所属栏目:Java 来源:网络整理
导读:我有一个控制器,提供下载文件的功能. @ResponseBodypublic void downloadRecycleResults(String batchName,HttpServletResponse response) throws Exception { File finalResultFile = null; // code here generates and initializes finalResultFile for ba
我有一个控制器,提供下载文件的功能.
@ResponseBody public void downloadRecycleResults(String batchName,HttpServletResponse response) throws Exception { File finalResultFile = null; // code here generates and initializes finalResultFile for batchName response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition","attachment;filename=" + finalResultFile.getName()); IOUtils.copy(new FileReader(finalResultFile),response.getOutputStream()); } 我无法弄清楚如何编写测试,我可以验证写入响应的内容.我已经使用了ArgumentCaptor很多但不知何故它似乎不适合这里. controller.downloadRecycleResults("batchName",mock(HttpServletResponse.class)); verify(response).getOutputStream(); // but how to capture content? 解决方法
一种方法是模拟响应及其输出流,然后验证对模拟输出流的write方法的调用.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |