English 中文(简体)
如何在WicketTester中测试AjaxFormChoiceComponentUpdatingBehavior
原标题:How to test AjaxFormChoiceComponentUpdatingBehavior in WicketTester

在我的Wicket应用程序中,我使用了一个带有“是”和“否”选项的单选按钮。如果我选择“否”,我应该显示一个下拉选项。我使用AjaxFormChoiceComponentUpdatingBehavior编写代码。如何使用WicketTester对此进行单元测试?

问题回答

Wicket 1.5.x的解决方案:

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

首先选择所需的单选按钮。

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

然后执行ajax行为:

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

这是我的一段代码,它非常适合我的选择框,但如果你改变行为类,它也应该适用于单选按钮。所需的步骤包括:

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

下面是一个代码示例:

//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);

我错过了之前答案中关于如何更新表单值的部分。

如果单选按钮在窗体上,我认为您应该使用FormTester类:

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

关于Ajax表单提交测试的示例,您可以查看:

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");

事实证明这有点痛苦,至少在Wicket 1.4中是这样(我还没有尝试过1.5)。

通过网络搜索,我在Mischa Dasberg的博客。基本上,你不能使用BaseWicketTester.executeAjaxEvent((String componentPath,String event)方法,因为你使用的行为不是AjaxEventBehavior,你也不能使用BaseWicketTester.executeBehavior(最终的AbstractAjaxBehavior行为),因为它会删除请求参数。

Mischa的解决方案是在父测试用例中实现他自己的executeBehavior方法,这适用于他的情况,但不适合我的需要,因为它假设请求参数id与完整组件路径相同。

我也做了类似的事情,在WicketTester的扩展中实现了我自己的executejaxBehavior,但假设(在我的情况下也是如此)请求参数是组件路径的最后一个“:”分隔部分:

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);
}

他的解决方案和我的解决方案(基于他的)都假设行为是组件上的第一个(或唯一一个)行为。

这有点笨重,但像这样的东西可能对你有用。

如果将id和行为分别获取并作为参数传递可能会更好,当然,您最好找到第一个实际上是AjaxFormChoiceComponentUpdatingBehavior的行为,而不是愉快地假设它是第一个行为,但这只是一个开始。

这也类似于其他行为测试方法的BaseWicketTester类中的代码,可能值得仔细研究。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签