A drop down control is handy when you wish to select one out of many and there is not enough room for a radio group.
Message: [Message]
The code for testing a drop down is very similar to that of a check box. But first a look at the implementation.
There is no AjaxDropDownChoice, instead you have to add an AjaxFormComponentUpdatingBehavior to the control and implement the behavior in the onUpdate method.
public class DropDownForm extends Form<DropDownFormObject> { private static final long serialVersionUID = 1L; public DropDownForm(String id) { super(id); DropDownFormObject model = new DropDownFormObject(); setDefaultModel(new CompoundPropertyModel<DropDownFormObject>(model)); DropDownChoice<String> dropDownChoice = new DropDownChoice<String>(DROPDOWN, Arrays.asList(choices)); dropDownChoice.add(createAjaxBehavior()); add(dropDownChoice); } private AjaxFormComponentUpdatingBehavior createAjaxBehavior() { AjaxFormComponentUpdatingBehavior updatingBehavior = new AjaxFormComponentUpdatingBehavior(ONCHANGE_EVENT) { private static final long serialVersionUID = 1L; @Override protected void onUpdate(AjaxRequestTarget target) { DropDownFormObject modelObject = (DropDownFormObject) getDefaultModelObject(); log.debug(String.format("Got a change: %s", modelObject.toString())); label.setDefaultModelObject(modelObject.toString()); ajaxUpdate(target, label); } }; return updatingBehavior; } @Override protected void onSubmit() { log.debug("onSubmit: " + getDefaultModelObjectAsString()); setResponsePage(RadioGroupPage.class); } }
Since we decide the string ourselves the ONCHANGE_EVENT is a constant that we can use in our tests.
Here is the slightly different form filling method of our test class.
private FormTester fillInTheForm() { FormTester formTester = tester.newFormTester(DropDownPage.FORM); formTester.select(DropDownPage.DROPDOWN, 0); tester.executeAjaxEvent(DropDownPage.FORM + ":" + DropDownPage.DROPDOWN, DropDownPage.ONCHANGE_EVENT); return formTester; }