Last week I ran into a problem while testing AjaxBehavior in Wicket.

Consider the following scenario: we have a FormComponent which has an AjaxBehavior added to it. We want to test that behavior. Depending on the selected value of for instance a RadioChoice, the Ajaxbehavior should show one component and hide another.

You would think that this would be out of the box behavior when using WicketTester, but unfortunately this is not the case. Triggering an Ajaxbehavior is easy using WicketTester, but setting the selected value isn’t.

So we have the following code:

The way you should normally expect the test to work is:

Unfortunately this doesn’t work because the WicketTester method executeBehavior calls
tester.getServletRequest().setRequestToRedirectString(url.toString()) which sets the
selected value to null.

To be able to test this behavior I created a method om my BaseTest class which looks like this:

Now you can test the behavior like this.