2
我有一个提供下载文件功能的控制器。如何使用ArgumentCaptor来验证写入HttpServletResponse的字节
@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());
}
我无法弄清楚如何编写测试,我可以确认这是写给response
内容。我已经使用了很多ArgumentCaptor
,但不知何故它似乎不适合这里。要做到这一点
controller.downloadRecycleResults("batchName", mock(HttpServletResponse.class));
verify(response).getOutputStream(); // but how to capture content?
谢谢你的想法。我在发布后也想到了这一点。我会发布代码作为答案,以防有人绊倒。 – RandomQuestion