如何在zipoutputstream中断言响应

时间:2017-03-10 10:19:38

标签: java unit-testing junit mockito spring-test-mvc

我正在尝试使用MockitoJUnitRunner编写JUnit。 我将文件ID传递给我的功能,即从云下载文件并返回zip文件作为下载。 这是我的代码

public void getLogFile(HttpServletResponse response, String id) throws IOException {

    response.setContentType("Content-type: application/zip");
    response.setHeader("Content-Disposition", "attachment; filename=LogFiles.zip");

    ServletOutputStream out = response.getOutputStream();
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(out));

    zos.putNextEntry(new ZipEntry(id));

    InputStream inputStream = someDao.getFile(id);

    BufferedInputStream fif = new BufferedInputStream(inputStream);

    int data = 0;

    while ((data = fif.read()) != -1) {
        zos.write(data);
    }

    fif.close();

    zos.closeEntry();
    zos.close();
}

我的JUnit功能是

@Mock
private MockHttpServletResponse mockHttpServletResponse;

anyInputStream = new ByteArrayInputStream("test data".getBytes());

    @Test
    public void shouldDownloadFile() throws IOException {

    ServletOutputStream outputStream = mock(ServletOutputStream.class);

        when(mockHttpServletResponse.getOutputStream()).thenReturn(outputStream);


 =>          when(someDao.download(anyString())).thenReturn(anyInputStream);


        controller.getLogFile(mockHttpServletResponse, id);

        verify(mockHttpServletResponse).setContentType("Content-type: application/zip");

        verify(mockHttpServletResponse).setHeader("Content-Disposition","attachment; filename=LogFiles.zip");

        verify(atmosdao).download(atmosFilePath);
    }

这个单元测试正在通过,但我想验证outputStream上写的是什么,我该怎么做?因为我正在写"测试数据"模拟outputStream,如

anyInputStream = new ByteArrayInputStream("test data".getBytes());

when(someDao.download(anyString())).thenReturn(anyInputStream);

mockHttpServletResponse.getContentAsString()给我空了!

是否可以断言使用zipoutputStream编写的MockHttpServletResponse?如果是,那我该怎么办呢?

感谢。

2 个答案:

答案 0 :(得分:1)

您可以创建自定义的OutputStream,而不是模仿您的public class CustomOutputStream extends ServletOutputStream { private ByteArrayOutputStream out = new ByteArrayOutputStream(); private String content; @Override public void write(int b) throws IOException { out.write(b); } @Override public void close() throws IOException { content = new String(out.toByteArray()); out.close(); super.close(); } public String getContentAsString() { return this.content; } }

content

该类将存储写入其中的所有字节,并将它们保存在ServletOutputStream outputStream = mock(ServletOutputStream.class); 字段中。

然后你替换它:

CustomOutputStream outputStream = new CustomOutputStream();

由此:

getOutputStream()

当你的servlet调用getContentAsString()时,它将使用自定义的一个,最后out.toByteArray()将返回写入你的servlet的输出。

注意:输出是压缩的,因此String将包含奇怪的字符。如果你想要原始的字符串,你必须解压缩它(在这种情况下我会使用string.getBytes()返回的字节数组而不是字符串,因为当你以这种方式创建一个字符串时,你可以进行编码调用function onEdit(e) { var limit = 5; if (e.value.length > limit && e.range.rowStart > 2 && e.range.rowStart < 101 && e.range.columnStart == 3) { e.range.setValue(e.value.substring(0, limit)); } }

时出现问题

答案 1 :(得分:1)

我得到了我想要的东西...... 这就是我使用powerMockito断言写入zipoutputStream的数据。

@Test
public void ShouldAttemptToWriteDownloadedFileToZipOutputStream() throws Exception {

    InputStream anyInputStream = new ByteArrayInputStream("test data".getBytes());

    ServletOutputStream outputStream = mock(ServletOutputStream.class);

    BufferedOutputStream bufferedOutputStream = Mockito.mock(BufferedOutputStream.class);
    PowerMockito.whenNew(BufferedOutputStream.class).withArguments(outputStream).thenReturn(bufferedOutputStream);

    ZipOutputStream zipOutputStream = Mockito.mock(ZipOutputStream.class);
    PowerMockito.whenNew(ZipOutputStream.class).withArguments(bufferedOutputStream).thenReturn(zipOutputStream);

    BufferedInputStream bufferedInputStream = new BufferedInputStream(anyInputStream);
    PowerMockito.whenNew(BufferedInputStream.class).withArguments(anyInputStream).thenReturn(bufferedInputStream);

    subjectUnderTest.getLogFile(mockHttpServletResponse, "12345");

    int data = 0;

    while ((data = bufferedInputStream.read()) != -1) {

        verify(zipOutputStream).write(data);
    }
}

感谢Hugo的帮助!