开发者

Testing Wicket panels with constructor arguments

I got a page with several panels that takes several parameters in their constructors. One of them being a menu that takes a list for the different buttons in the menu.

I've tried to test it as components but get a null pointer exception. Using a dummy page and creating the panel on the dummy page works. I'm not entirely happy with this approach si开发者_运维百科nce it introduces a lot of new code in my tests and more possibilities for errors.

Is there a better way of testing panels that takes arguments in their constructor?


Sure thing: The code that gives an null pointer error:

public void testVisitPanel(){
    VisitPanel v = new VisitPanel("visitPanel");
    tester.startComponent(v);
    tester.assertContains("DATE");
}

The panel

public VisitPanel(String id) {
    super(id);
    add( new Label("visitDate", "DATE"));
    add( new Label("visitStage", "VISIT SIGNED"));
}

And the html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:wicket="http://wicket.sourceforge.net/" 
    xml:lang="en" 
    lang="en">  
<wicket:head>
</wicket:head>

<body>
    <wicket:panel>
        <span wicket:id="visitDate">VISIT DATE</span>
        <span wicket:id="visitStage">STAGE</span>
    </wicket:panel>
</body>
</html>


If you're using Wicket 1.4, you probably want wicketTester.startPanel.

What I do is create an implementation of ITestPanelSource in the test, doing something like:

private class TestPanelSource implements ITestPanelSource {

    private static final long serialVersionUID = 1L;

    public Panel getTestPanel(String panelId) {
        return new MyPanel(panelId, myArg1, myArg2);
    }

}

with the myArgN being fields in the test class (frequently mocks) that suit the constructor, and then call it in the test or in a setUp method with

wicketTester.startPanel(new TestPanelSource());

This is basically doing some of the DummyPage work for you, so it may not be that far from what you're doing now, but might at least save on implementation of dummy pages for test infrastructure.

In Wicket 1.5, this is deprecated in favor the component testing that you referenced in the question. That should also work, so it might be worthwhile to post some actual code that is giving you trouble with that technique.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜