wicket 9:如何测试下载资源

wicket 9: how to test downloading a resource

我有一个组件允许用户在单击 link 后下载 excel 文件。 它工作正常,一切都很好,但我不知道如何为这个组件编写测试。 我想写一个测试来检查在按下 link 之后文件是否被发送到客户端。

因此,我的组件如下所示

Link<Void> calculationsLink = new Link<>("calculationsLink") {
    @Override
    public void onClick() {

        AbstractResourceStreamWriter rStream =
                new AbstractResourceStreamWriter() {

                    @Override
                    public void write(OutputStream output)
                            throws IOException {
                        output.write(MyApp.class
                                .getResourceAsStream(pathToCalculations)
                                .readAllBytes());
                    }
                };

        ResourceStreamRequestHandler handler =
                new ResourceStreamRequestHandler(rStream, "calculations.xslx");
        getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
    }
};

我的测试是

@Test
public void calculations_file_downloaded_Successfully() {
    // then start and render the base page
    tester.startPage(HomePage.class); <-- link is located in a HomePage

    tester.clickLink("navBar:calculations", false); <-- link is clickable 
    tester.getResponse();//????

    tester.assert???(?????); <-- how to assert and what to assert?
}

您应该使用 tester.getLastResponse() 并对其属性进行断言。

tester.getResponse() 是将用于下一次 HTTP 调用的 MockHttpServletResponse。

一些虚拟示例:

assertEquals("application/octet-stream", tester.getLastResponse().getContentType());
assertEquals(3, tester.getLastResponse().getBinaryContent().length);
assertArrayEquals(new byte[] {1, 2, 3}, tester.getLastResponse().getBinaryContent());