检票口测试中的 PageExpiredException

PageExpiredException in wicket's test

我已经将 wicket 从 8.10 更新到 8.11,现在我在单元测试中有很多失败。所有失败的测试都失败并出现一个错误:

@Test
public void testPanel() {
    Panel panel = new MyPanel("id");

    getTester().startComponentInPage(panel); // fails with PageExpiredException: Page with id '0' has expired.
}

没有可用的堆栈跟踪,控制台显示以下日志:

15:35:19.300 [main] WARN  RequestCycleExtra - ********************************
15:35:19.300 [main] WARN  RequestCycleExtra - Handling the following exception
org.apache.wicket.protocol.http.PageExpiredException: Page with id '0' has expired.
15:35:19.300 [main] WARN  RequestCycleExtra - ********************************

org.apache.wicket.protocol.http.PageExpiredException: Page with id '0' has expired.

因此,我在隔离中测试组件的所有测试都失败了。测试,我 运行 页面包含以下代码,没问题:

getTester().startPage(pageClass, getPageParameters());

在 wicket 8.10 中,所有测试都顺利通过。

有什么问题吗?这是错误还是我遗漏了应用程序配置中的某些内容?

更新:

这里描述了这个错误: https://issues.apache.org/jira/browse/WICKET-6856

每次测试前session失效,异常原因如下:

@Before
public void prepare() {
    logout(); // If this line is commented the error is not appears
}

protected void logout() {
    Session.get().signOut();
    application.getSecuritySettings().getAuthenticationStrategy().remove();
}

更新二:

解决方法是在注销代码中添加 Session.invalidateNow():

protected void logout() {
    Session.get().signOut();
    application.getSecuritySettings().getAuthenticationStrategy().remove();
    Session.get().invalidateNow(); // with this line the error is not appears
}

https://issues.apache.org/jira/browse/WICKET-6856 中所述,错误测试现在在 Wicket 8.11.0 中失败。

在#signOut()(调用#invalidate())之后,会话无效,用于呈现有状态页面的重定向将在测试中正常失败。

您必须调用#invalidateNow(),正如您在更新中所指出的那样。