【问题标题】:Set a Page for Component Testing with Apache Wicket使用 Apache Wicket 设置组件测试页面
【发布时间】:2016-02-24 13:05:57
【问题描述】:

我正在我的 wicket 7 应用程序中测试一些组件。

我的组件没什么特别的,但它继承自

public PageAwarePanel extends Panel {
    @Override
    protected void onInitialize() {
        super.onInitialize();

        //refuse if used on page without PageConfig
        if (getPageConfigurationModel() == null){
            throw new RuntimeException("this component is only allowed inside pages having PageConfigurationModel");
        }
    }

    protected IModel<PageConfiguration> getPageConfigurationModel(){
        if (getPage() instanceof TemplatePage){
            return ((TemplatePage)getPage()).getPageConfigurationModel();
        }               
        return null;
    } 
}

有了这个,我可以从某个面板访问一些配置。

现在当我尝试测试时:

    PositionsPanel p = new PositionsPanel("123", asmNumber, Model.of());
    tester.startPage(MyPage.class);
    tester.startComponentInPage(p);

其中 MyPage 是一个 TemplatePage。

我得到了定义的 RuntimeException。我的问题是:

如何通过定义应该在哪个页面上呈现该组件来测试该组件?

感谢所有高级帮助。

【问题讨论】:

    标签: java wicket wicket-tester


    【解决方案1】:
    tester.startPage(MyPage.class);
    tester.startComponentInPage(p);
    

    这两个没有关系。这就像在浏览器中浏览两个不同的页面。

    最好的方法是为满足要求的测试创建一个页面,将面板添加到此页面并执行tester.startPage(TestPage.class)

    另一种方法是覆盖org.apache.wicket.util.tester.BaseWicketTester#createPage() 并返回MyPage 的实例。这样你仍然可以使用startComponentInPage(),但其他方面与第一种方法基本相同。

    【讨论】:

    • 谢谢你,马丁,第一个描述的方法对我有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多