Wicket testing startPanel(Panel) and PageParameter

2019-07-27 00:52发布

问题:

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?

回答1:

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.



回答2:

Personally, I think, getting the PageParameters via getPage().getPageParameters() is a suboptimal way to go.

It introduces a dependency from your panel to your page which makes them tightly coupled and hard to test, as you can see.

If you need to access the PageParameters from within your Panel, inject them by creating a constructor accepting these, something along the lines of

private PageParameters params;

public MyPanel (String id, Model<T> model, PageParameters params) {
    super(id, model);
    this.params = params;
    ...
}

This way you can use the Parameters from within your Panel, construct the Panel with any PageParameters you can imagine in your tests and you avoided the coupling that proved to be disadvantageous.



回答3:

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.



标签: wicket