我有一个控制器,提供下载文件的功能.
@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());
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何编写测试,我可以验证写入的内容response.我已经使用了ArgumentCaptor很多但不知何故它似乎不适合这里.
controller.downloadRecycleResults("batchName", mock(HttpServletResponse.class));
verify(response).getOutputStream(); // but how to capture content?
Run Code Online (Sandbox Code Playgroud)
Ran*_*ion 10
正如大卫所说,我能够这样做.
ServletOutputStream opStreamMock = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(opStreamMock);
ArgumentCaptor<byte[]> captor = ArgumentCaptor.forClass(byte[].class);
verify(opStreamMock).write(captor.capture(), Mockito.anyInt(), Mockito.anyInt());
//can create reader now.
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(captor.getValue())));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4825 次 |
| 最近记录: |