Loading...

May 25, 2009

Test selection of radio choice options in Wicket

If we want to test the selection of a radio choice we must take some extra steps. After reading this JIRA issue we have a solution:

package com.mrhaki.wicket;

import org.apache.wicket.Component;
import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Test;

public class PageTest {
  @Test
  public void testRadioChoice() {
    final WicketTester tester = new WicketTester();
    tester.startPage(Page.class);
    final FormTester form = tester.newFormTester("form");
    form.select("radiochoice", 0);
    form.getForm().visitChildren(RadioChoice.class, new IVisitor() {
      public Object component(final Component component) {
        if (component.getId().equals("radiochoice")) {
          ((RadioChoice) component).onSelectionChanged();
        }
        return CONTINUE_TRAVERSAL;
      }
    });
    
    // Do assertion checks here
    // ...
  }
}