wicketwicket-tester

unit-testing Wicket input components


I just wrote my first Wicket component :) It contains a ListView with some Radio input fields. Now I want to unit test if a selected value makes its way to the model.

As WicketTester.newFormTester("myForm") expects a form, I try to create a form on the fly:

public void testDataBinding()
{
    Model model = ...
    MyRadioComponent myRadioComponent = new MyRadioComponent (...);
    Form form = new Form("myForm", ...);
    form.add(myRadioComponent);
    WicketTester wicketTester = new WicketTester();
    wicketTester.startComponentInPage(form);
    // FormTester formTester = wicketTester.newFormTester("myForm");
    // ...
}

Now wicketTester.startComponentInPage(form) results in:

Failed: Component [myForm] (path = [0:x]) must be applied to a tag of type [form], 
        not: '<span wicket:id="myForm" id="myForm3">'

Any idea how to fix this and/or how to test such an input component the right way?


Solution

  • I believe startComponentInPage uses a <span> for its component. Wicket checks that Forms are attached to <form> tags which is why you get this error.

    You need to create your own test page with a <form> inside it. See org.apache.wicket.markup.html.form.NumberTextFieldTest for an example of inline markup. Otherwise, create a Form test page class with associated html markup file.