2

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?

Marcus
  • 1,857
  • 4
  • 22
  • 44

2 Answers2

4

OK, in detail the solution now looks like this:

public FormTester createFormTester(Component c) {
    final WicketTester wicketTester = new WicketTester();
    final FormPage page = new FormPage(c);
    wicketTester.startPage(page);
    return wicketTester.newFormTester(page.getPathToForm());
}

private static class FormPage extends WebPage implements IMarkupResourceStreamProvider {

    private final Form<Void> form;
    private final Component c;

    private FormPage(final Component c) {
        this.c = c;
        add(form = new Form<>("form"));
        form.add(c);
    }

    public String getPathToForm() {
        return form.getPageRelativePath();
    }

    @Override
    public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass) {
        return new StringResourceStream(
                "<html><body>"
                + "<form wicket:id='" + form.getId() + "'><span wicket:id='" + c.getId() + "'/></form>"
                + "</body></html>");
    }
}
Marcus
  • 1,857
  • 4
  • 22
  • 44
3

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.

cringe
  • 13,401
  • 15
  • 69
  • 102
bernie
  • 9,820
  • 5
  • 62
  • 92