1

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 since 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>
Community
  • 1
  • 1
Alvin
  • 178
  • 8

1 Answers1

3

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.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
  • Thanks! Using component testing works. However some of the panels gives me a null pointer exception. Testing things around a bit it seems like my panels that uses the default constructor with only Id as an argument does not work with component testing. Using startPanel does work though (startPanel(Panel.class)). So mixing these two I can test all of my panels. – Alvin Oct 18 '11 at 14:12
  • I'm glad this was helpful. If you post code for an example of something that gives a null pointer when doing component testing, perhaps we can help more. – Don Roby Oct 18 '11 at 14:22
  • Posted code in the question since i couldn't fit it in the comment. – Alvin Oct 19 '11 at 06:41