如何设置自定义会话与检票口时,单元测试? [英] How do you set a custom session when unit testing with wicket?

查看:151
本文介绍了如何设置自定义会话与检票口时,单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一个检票口页面,只允许访问你登录后一些单元测试。在我的JUnit测试我无法启动页面或使其不设置会话。

你如何设置会话?我在寻找如何做到这一点的任何文档的问题。

  WicketTester测试仪=新WicketTester(新MyApp的());
((MyCustomSession)tester.getWicketSession())setItem(MyFactory.getItem(ABC));//未能启动以下,无会话似乎被设置
    tester.startPage(General.class);
tester.assertRenderedPage(General.class);


解决方案

我经常做的是提供一个假的WebApplication与覆盖的东西,我想嘲笑或存根。

在这些事情我是重写方法

 公共抽象会话newsession的(请求请求,响应响应);

这允许您用任何你想要返回一个假的会话设置。

这是在检票1.3 - 如果你使用1.4,其中一些可能已经改变,作为另一个回应指出,这可能与一个检票口错误

不过,假设该接口并没有改变太多,重写此方法也可能是便门-1215解决此问题的工作的另一种方法。

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);

解决方案

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.

这篇关于如何设置自定义会话与检票口时,单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆