tags:

views:

271

answers:

2

Currently I'm using the WicketTester's startPanel method to test my panels. Within these panels I often use PageParameters to access data, using getPage().getPageParameters(). However, the startPanel method does not initialize any page parameters for the DummyPage, nor does it offer me functionality to set page parameters.

How do I set my page parameters during panel tests?

A: 

You could use an anonymous (or subclass) of TestPanelSource with the page and panel already created.

final WebPage parameterPage = new WebPage(pageParmeters);
final Panel panelUnderTest = .... create panel here ....;
parameterPage.add(panelUnderTest);
wicketTester.startPanel(new TestPanelSource(){
    public Panel getTestPanel(final String panelId) {
        return panelUnderTest;
    }
});

If you use this a lot a subclass of TestPanelSource might be best to be able to pass the parameters into your sub-class' constructor and other configurable items of your interest.

Matt
+1  A: 

Is that really the way to do it? The startPanel already creates a dummy page for us, but without any page parameters. Using your approach attaches the panel to two pages, which does not seem like the optimal solution to me. Right now I extended the WicketTester with a startPanel(Panel, PageParameters) function:

public Panel startPanel(final TestPanelSource testPanelSource, final PageParameters parameters) {
    return (Panel) startPage(new ITestPageSource() {
        public Page getTestPage() {
            return new DummyPanelPage(testPanelSource, parameters);
        }
    }).get(DummyPanelPage.TEST_PANEL_ID);
}

And created a new dummy panel page with a page parameters constrctor

public class DummyPanelPage extends WebPage {

    /** 
     * The dummy <code>Panel</code> <code>Component</code> id.
     */
    public static final String TEST_PANEL_ID = "panel";

    /**
     * Default constructor.
     * @param testPanelSource <code>TestPanelSource</code>
     */
    public DummyPanelPage(final TestPanelSource testPanelSource, final PageParameters parameters) {
        super(parameters);
        add(testPanelSource.getTestPanel(TEST_PANEL_ID));
    }

}

It beats me why this functionality isn't just provided out of the box by Apache.

Jeroen
+1My first instinct would have been to do a startPage instead of startPanel and use a test page constructed to handle the PageParameters.But your solution is better.
Don Roby
+1 I didn't realize that it started it's own page. This is closer to how I would write your tests then Jeroen.
Matt