开发者

How to test AjaxFormChoiceComponentUpdatingBehavior in WicketTester

In my Wicket application I used one radio button with "yes" and "no" options. If I select "No", I should display one dropdown choice. I wrote c开发者_如何学Goode using AjaxFormChoiceComponentUpdatingBehavior. How do I unittest this using WicketTester?


Solution for Wicket 1.5.x:

 AbstractAjaxBehavior behavior = (AbstractAjaxBehavior)WicketTesterHelper.
          findBehavior(getTester().getComponentFromLastRenderedPage("path:to:component"),
                        AjaxFormChoiceComponentUpdatingBehavior.class);
 getTester().executeBehavior(behavior);


First select the radio button that you want.

form.select("path to radio button", 0/1)

Then execute ajax behaviour:

tester.executeBehavior((AbstractAjaxBehavior)tester.getComponentFromLastRenderedPage("path to radio buttons").getBehaviors().get(0));


Here is my piece of code which works perfectly for me with select box but should fiat as well for radio button if you change Behaviour class. Needed steps are:

  • Insert new value into form (use FormTester)
  • Find behaviour
  • Execute behaviour on change

Here is an example of code:

//simulate insert new value
FormTester formTester = tester.newFormTester(PANEL_ID + FORM); 
formTester.setValue("selectBox", "newValue");
//Find onchange behaviour
AjaxFormComponentUpdatingBehavior behavior = 
       (AjaxFormComponentUpdatingBehavior) WicketTesterHelper.findBehavior(
       tester.getComponentFromLastRenderedPage(PANEL_ID + FORM + ":" + "selectBox"), 
       ajaxFormComponentUpdatingBehavior.class);
//execute onchange
tester.executeBehavior(behavior);

I missed the par how to update form value in previous answers.


If the radio button is on a form I think you should use the FormTester class:

http://wicket.apache.org/apidocs/1.4/org/apache/wicket/util/tester/FormTester.html

For an example of an Ajax form submit test you can take a look at:

http://www.java2s.com/Open-Source/Java-Document/J2EE/wicket-1.4/org/apache/wicket/ajax/form/AjaxFormSubmitTest.java.htm


Try something like this: tester.executeAjaxEvent("form:myRadioButtonId", "onchange");


This turns out to be somewhat painful, at least in Wicket 1.4 (I haven't tried with 1.5).

Via a web search, I found hints in Mischa Dasberg's blog. Basically, you can't use the BaseWicketTester.executeAjaxEvent((String componentPath, String event) method because the behavior you're using isn't an AjaxEventBehavior and you can't use the BaseWicketTester.executeBehavior(final AbstractAjaxBehavior behavior) because it wipes out the request parameters.

Mischa's solution was to implement his own executeBehavior method in a parent test case, which worked for his situation, but not for my need, as it assumed the request parameter id was the same as the full component path.

I've done something similar by implementing my own executeAjaxBehavior in an extension of WicketTester, but assuming (as is true in my case) that the request parameter is the last ":" separated section of the component path:

public void executeAjaxBehavior(String path, String value) {
    AbstractAjaxBehavior behavior = (AbstractAjaxBehavior) getComponentFromLastRenderedPage(path).getBehaviors().get(0);
    CharSequence url = behavior.getCallbackUrl(false);
    WebRequestCycle cycle = setupRequestAndResponse(true);
    getServletRequest().setRequestToRedirectString(url.toString());
    String[] ids = path.split(":");
    String id = ids[ids.length-1];
    getServletRequest().setParameter(id, value);
    processRequestCycle(cycle);
}

Both his solution and mine (based on his) also assume that the behavior is the first (or only) one on the component.

This is a bit clunky, but something like this may work for you.

It might be better if the ids and behavior were gotten separately and passed as parameters, and of course you might do well to find the first behavior that actually was an AjaxFormChoiceComponentUpdatingBehavior instead of blithely assuming it was the first behavior, but this is a start.

This is also similar code to what's inside the BaseWicketTester class for the other behavior testing methods, which might be worth looking through.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜