1

我正在我的 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。我的问题是:

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

感谢所有高级帮助。

4

1 回答 1

2
tester.startPage(MyPage.class);
tester.startComponentInPage(p);

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

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

另一种方法是覆盖org.apache.wicket.util.tester.BaseWicketTester#createPage()并返回MyPage. 这种方式你仍然可以使用startComponentInPage(),但除此之外它与第一种方法基本相同。

于 2016-02-24T13:54:21.063 回答