使用 wicket 进行单元测试时如何设置自定义会话?

发布于 2024-08-25 22:49:42 字数 411 浏览 8 评论 0原文

我正在尝试在仅允许登录后访问的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够钟 2024-09-01 22:49:42

我经常做的是提供一个假的 Web 应用程序,并覆盖我想要模拟或存根的内容。

我重写的方法之一是

    public abstract Session newSession(Request request, Response response);

允许您返回带有任何您想要的内容的虚假会话设置。

这是 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

    public abstract Session newSession(Request request, Response response);

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.

萌面超妹 2024-09-01 22:49:42

您可能会遇到 WICKET-1215。否则你所做的看起来很好。例如,我有一个 Junit4 设置方法,如下所示:

@Before
public void createTester() {
    tester = new WicketTester( new MyApp() );
    // see http://issues.apache.org/jira/browse/WICKET-1215
    tester.setupRequestAndResponse();
    MyAppSession session = (MyAppSession) tester.getWicketSession();
    session.setLocale(Locale.CANADA);
    session.setUser(...);
}

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:

@Before
public void createTester() {
    tester = new WicketTester( new MyApp() );
    // see http://issues.apache.org/jira/browse/WICKET-1215
    tester.setupRequestAndResponse();
    MyAppSession session = (MyAppSession) tester.getWicketSession();
    session.setLocale(Locale.CANADA);
    session.setUser(...);
}
贪恋 2024-09-01 22:49:42

使用 Wicket 1.4,我使用正常的 WebApplication 和 WebSession 实现,在我的应用程序中称为 NewtEditor 和 NewtSession。我重写了 newSession,其中的操作与常规应用程序代码中的操作相同,只是我立即登录。出于性能原因,我还重写了 newSessionStore,我从 WicketTesters 代码中复制了这个技巧。

tester = new WicketTester(new NewtEditor() 
{
    @Override
    public Session newSession(Request request, Response response)
    {
        NewtSession session = new NewtSession(request);
        session.signIn(getTestDao());
        return session;
    }

    @Override
    protected ISessionStore newSessionStore()
    {
        // Copied from WicketTester: Don't use a filestore, or we spawn lots of threads,
        // which makes things slow.
        return new HttpSessionStore(this);
    }
});

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.

tester = new WicketTester(new NewtEditor() 
{
    @Override
    public Session newSession(Request request, Response response)
    {
        NewtSession session = new NewtSession(request);
        session.signIn(getTestDao());
        return session;
    }

    @Override
    protected ISessionStore newSessionStore()
    {
        // Copied from WicketTester: Don't use a filestore, or we spawn lots of threads,
        // which makes things slow.
        return new HttpSessionStore(this);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文