javawicketwicket-tester

Set a Page for Component Testing with Apache Wicket


I am testing some components in my wicket 7 application.

My Component is nothing special but it inherits from

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

With this I can access some configurations from a certain panel.

Now when I try in a test:

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

where MyPage is a TemplatePage.

I get the defined RuntimeException. My Question is:

How can I test this component with defining on which page it should be rendered?

Thanks for all the help in advanced.


Solution

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

    those two are not related. It is like navigating two different pages in the browser.

    The best way is to create a page for the tests that fulfills the requirements, add the panel to this page and do tester.startPage(TestPage.class).

    Another way is to override org.apache.wicket.util.tester.BaseWicketTester#createPage() and return an instance of MyPage. This way you can still use startComponentInPage() but otherwise it is basically the same as the first approach.