gpt4 book ai didi

wicket - 单元测试 Wicket 输入组件

转载 作者:行者123 更新时间:2023-12-02 08:34:37 30 4
gpt4 key购买 nike

我刚刚编写了我的第一个 Wicket 组件 :) 它包含一个 ListView 和一些 Radio 输入字段。现在我想对所选值是否进入模型进行单元测试。

由于 WicketTester.newFormTester("myForm") 需要一个表单,我尝试动态创建一个表单:

public void testDataBinding()
{
Model model = ...
MyRadioComponent myRadioComponent = new MyRadioComponent (...);
Form form = new Form("myForm", ...);
form.add(myRadioComponent);
WicketTester wicketTester = new WicketTester();
wicketTester.startComponentInPage(form);
// FormTester formTester = wicketTester.newFormTester("myForm");
// ...
}

现在 wicketTester.startComponentInPage(form) 结果:

Failed: Component [myForm] (path = [0:x]) must be applied to a tag of type [form], 
not: '<span wicket:id="myForm" id="myForm3">'

知道如何解决这个问题和/或如何以正确的方式测试这样的输入组件吗?

最佳答案

好的,详细的解决方案现在看起来是这样的:

public FormTester createFormTester(Component c) {
final WicketTester wicketTester = new WicketTester();
final FormPage page = new FormPage(c);
wicketTester.startPage(page);
return wicketTester.newFormTester(page.getPathToForm());
}

private static class FormPage extends WebPage implements IMarkupResourceStreamProvider {

private final Form<Void> form;
private final Component c;

private FormPage(final Component c) {
this.c = c;
add(form = new Form<>("form"));
form.add(c);
}

public String getPathToForm() {
return form.getPageRelativePath();
}

@Override
public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass) {
return new StringResourceStream(
"<html><body>"
+ "<form wicket:id='" + form.getId() + "'><span wicket:id='" + c.getId() + "'/></form>"
+ "</body></html>");
}
}

关于wicket - 单元测试 Wicket 输入组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23137308/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com