【问题标题】:wicket 9: how to test downloading a resourcewicket 9:如何测试下载资源
【发布时间】:2021-12-07 19:03:09
【问题描述】:

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

所以,我的组件看起来像这样

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?
}

【问题讨论】:

    标签: wicket


    【解决方案1】:

    您应该使用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());
    

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 2018-05-30
      相关资源
      最近更新 更多