使用 wicket 进行单元测试时如何设置自定义会话?
我正在尝试在仅允许登录后访问的 wicket 页面上运行一些单元测试。在我的 JUnit 测试中,如果不设置会话,我无法启动页面或呈现它。
你如何设置会话?我在查找有关如何执行此操作的任何文档时遇到问题。
WicketTester tester = new WicketTester(new MyApp());
((MyCustomSession)tester.getWicketSession()).setItem(MyFactory.getItem("abc"));
//Fails to start below, no session seems to be set
tester.startPage(General.class);
tester.assertRenderedPage(General.class);
I'm trying to run some unit tests on a wicket page that only allows access after you've logged in. In my JUnit test I cannot start the page or render it without setting the session.
How do you set the session? I'm having problems finding any documentation on how to do this.
WicketTester tester = new WicketTester(new MyApp());
((MyCustomSession)tester.getWicketSession()).setItem(MyFactory.getItem("abc"));
//Fails to start below, no session seems to be set
tester.startPage(General.class);
tester.assertRenderedPage(General.class);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我经常做的是提供一个假的 Web 应用程序,并覆盖我想要模拟或存根的内容。
我重写的方法之一是
允许您返回带有任何您想要的内容的虚假会话设置。
这是 Wicket 1.3 中的内容 - 如果您使用 1.4,其中一些内容可能已更改,并且如另一个响应中所述,它可能与 wicket bug 有关。
但假设接口没有发生太大变化,覆盖此方法也可能是解决 WICKET-1215 中问题的另一种方法。
What I frequently do is to provide a fake WebApplication with overrides for things that I want to mock or stub.
Among the things I override is the method
which allows you to return a fake session setup with anything you want.
This is in Wicket 1.3 - if you're using 1.4, some of this may have changed, and as noted in another response, it may be related to a wicket bug.
But assuming the interface hasn't changed too much, overriding this method may also be another way of working around the issue in WICKET-1215.
您可能会遇到 WICKET-1215。否则你所做的看起来很好。例如,我有一个 Junit4 设置方法,如下所示:
You may be running into WICKET-1215. Otherwise what you're doing looks fine. For example, I have a Junit4 setup method that looks like:
使用 Wicket 1.4,我使用正常的 WebApplication 和 WebSession 实现,在我的应用程序中称为 NewtEditor 和 NewtSession。我重写了 newSession,其中的操作与常规应用程序代码中的操作相同,只是我立即登录。出于性能原因,我还重写了 newSessionStore,我从 WicketTesters 代码中复制了这个技巧。
Using Wicket 1.4, I use my normal WebApplication and WebSession implementations, called NewtEditor and NewtSession in my app. I override newSession, where I do the same than in the regular app code, except that I sign in right away. I also override newSessionStore for performance reasons, I copied this trick from WicketTesters code.