如何在 WicketTester 中测试 AjaxFormChoiceComponentUpdatingBehavior
在我的 Wicket 应用程序中,我使用了一个带有“是”和“否”选项的单选按钮。如果我选择“否”,我应该显示一个下拉选项。我使用 AjaxFormChoiceComponentUpdatingBehavior
编写了代码。如何使用 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 code using AjaxFormChoiceComponentUpdatingBehavior
. How do I unittest this using WicketTester?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Wicket 1.5.x 的解决方案:
Solution for Wicket 1.5.x:
首先选择您想要的单选按钮。
然后执行ajax行为:
First select the radio button that you want.
Then execute ajax behaviour:
这是我的一段代码,它非常适合我使用选择框,但如果您更改行为类,也应该适用于单选按钮。所需的步骤是:
这是一个代码示例:
我错过了之前答案中如何更新表单值的部分。
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:
Here is an example of code:
I missed the par how to update form value in previous answers.
如果单选按钮位于表单上,我认为您应该使用 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
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
尝试这样的事情:
tester.executeAjaxEvent("form:myRadioButtonId", "onchange");
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(final AbstractAjaxBehavior 行为),因为它会清除请求参数。Mischa的解决方案是在父测试用例中实现他自己的executeBehavior 方法,该方法有效。适合他的情况,但不适合我的需要,因为它假设请求参数 id 与完整组件路径相同,
我通过在
扩展中实现我自己的
,但假设(在我的例子中也是如此)请求参数是组件路径的最后一个“:”分隔部分:executeAjaxBehavior
来完成类似的操作 。 >WicketTester他的解决方案和我的(基于他的)也假设行为是组件上的第一个(或唯一一个)
这有点笨拙,但类似的东西可能对您有用。
如果单独获取 id 和行为并将其作为参数传递可能会更好,当然,您可能会更好地找到实际上是 AjaxFormChoiceComponentUpdatingBehavior 的第一个行为,而不是轻松地假设它是第一个行为,但这只是一个开始。
这也与其他行为测试方法的 BaseWicketTester 类中的代码类似,可能值得仔细研究。
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 anAjaxEventBehavior
and you can't use theBaseWicketTester.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 ofWicketTester
, but assuming (as is true in my case) that the request parameter is the last ":" separated section of the component path: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.