如何使用 wicket 编写检查空输入值的单元测试
How to write unit testing checking null input values using wicket
我正忙于 wicket 应用程序,需要进行单元测试,我有一个登录页面可以验证来自 List<Customer>
的输入,但我不知道从哪里开始,我确实尝试了一些,但它给了我错误,只是不知道我在做什么任何帮助表示赞赏
Homepage.java
form.add(new AjaxButton("error") {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target)
{
for (Customer item: lstCustomer){
if (item.name.equals(username.getInput()) && item.password.equals(password.getInput())){
final String usernameValue = username.getModelObject();
PageParameters pp = new PageParameters();
pp.add("username", usernameValue);
setResponsePage(SuccessPage.class, pp);
valid[0] = true;
break;
}else {
logger.error("In the else");
valid[0] = false ;
}
}
if ( valid[0] == false){
errorDialog.open(target);
}
}
});
测试页面
private WicketTester tester;
@Before
public void setUp()
{
tester = new WicketTester(new WicketApplication());
}
@Test
public void homepageRendersSuccessfully()
{
//start and render the test page
tester.startPage(HomePage.class);
//assert rendered page class
tester.assertRenderedPage(HomePage.class);
}
@Test
public void validLogin(){
FormTester form = tester.newFormTester("userForm");
form.setValue("username", "emile");
form.setValue("password", "123");
form.submit();
tester.assertRenderedPage(SuccessPage.class);
}
然后我得到这个错误:
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-b020ca32-fe17-43e9-8cc5-3b7a618b2f93] init: Wicket extensions initializer
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-b020ca32-fe17-43e9-8cc5-3b7a618b2f93] init: Wicket jQuery UI initializer
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-b020ca32-fe17-43e9-8cc5-3b7a618b2f93] init: Wicket jQuery UI initializer (theme-uilightness)
java.lang.NullPointerException
at org.apache.wicket.util.tester.BaseWicketTester.getComponentFromLastRenderedPage(BaseWicketTester.java:1593)
at org.apache.wicket.util.tester.BaseWicketTester.getComponentFromLastRenderedPage(BaseWicketTester.java:1516)
at org.apache.wicket.util.tester.BaseWicketTester.getComponentFromLastRenderedPage(BaseWicketTester.java:1625)
at org.apache.wicket.util.tester.BaseWicketTester.newFormTester(BaseWicketTester.java:1311)
at org.apache.wicket.util.tester.BaseWicketTester.newFormTester(BaseWicketTester.java:1295)
at com.mycompany.TestHomePage.validLogin(TestHomePage.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
在您的 validLogin()
方法中,您需要从 tester.startPage(SomePage.class)
开始。
startPage()
与在浏览器中导航到某个地址相同。如果不先导航到其登录页面,您将无法登录到某些 Web 应用程序。
您也可以使用 #startComponentInPage(LoginPanel.class)
。此处 Wicket 将为您将您的登录面板添加到一个虚拟页面中。您可以断言 LoginPanel 以您需要的方式运行。
这样您就可以测试任何类型的 Wicket 组件,而不仅仅是面板。
您必须在创建 formTester 之前在 validLogin() 中设置起始页:
@Test
public void validLogin(){
//here...
tester.startPage(HomePage.class);
FormTester form = tester.newFormTester("userForm");
form.setValue("username", "emile");
form.setValue("password", "123");
form.submit();
tester.assertRenderedPage(SuccessPage.class);
}
我正忙于 wicket 应用程序,需要进行单元测试,我有一个登录页面可以验证来自 List<Customer>
的输入,但我不知道从哪里开始,我确实尝试了一些,但它给了我错误,只是不知道我在做什么任何帮助表示赞赏
Homepage.java
form.add(new AjaxButton("error") {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target)
{
for (Customer item: lstCustomer){
if (item.name.equals(username.getInput()) && item.password.equals(password.getInput())){
final String usernameValue = username.getModelObject();
PageParameters pp = new PageParameters();
pp.add("username", usernameValue);
setResponsePage(SuccessPage.class, pp);
valid[0] = true;
break;
}else {
logger.error("In the else");
valid[0] = false ;
}
}
if ( valid[0] == false){
errorDialog.open(target);
}
}
});
测试页面
private WicketTester tester;
@Before
public void setUp()
{
tester = new WicketTester(new WicketApplication());
}
@Test
public void homepageRendersSuccessfully()
{
//start and render the test page
tester.startPage(HomePage.class);
//assert rendered page class
tester.assertRenderedPage(HomePage.class);
}
@Test
public void validLogin(){
FormTester form = tester.newFormTester("userForm");
form.setValue("username", "emile");
form.setValue("password", "123");
form.submit();
tester.assertRenderedPage(SuccessPage.class);
}
然后我得到这个错误:
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-b020ca32-fe17-43e9-8cc5-3b7a618b2f93] init: Wicket extensions initializer
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-b020ca32-fe17-43e9-8cc5-3b7a618b2f93] init: Wicket jQuery UI initializer
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-b020ca32-fe17-43e9-8cc5-3b7a618b2f93] init: Wicket jQuery UI initializer (theme-uilightness)
java.lang.NullPointerException
at org.apache.wicket.util.tester.BaseWicketTester.getComponentFromLastRenderedPage(BaseWicketTester.java:1593)
at org.apache.wicket.util.tester.BaseWicketTester.getComponentFromLastRenderedPage(BaseWicketTester.java:1516)
at org.apache.wicket.util.tester.BaseWicketTester.getComponentFromLastRenderedPage(BaseWicketTester.java:1625)
at org.apache.wicket.util.tester.BaseWicketTester.newFormTester(BaseWicketTester.java:1311)
at org.apache.wicket.util.tester.BaseWicketTester.newFormTester(BaseWicketTester.java:1295)
at com.mycompany.TestHomePage.validLogin(TestHomePage.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
在您的 validLogin()
方法中,您需要从 tester.startPage(SomePage.class)
开始。
startPage()
与在浏览器中导航到某个地址相同。如果不先导航到其登录页面,您将无法登录到某些 Web 应用程序。
您也可以使用 #startComponentInPage(LoginPanel.class)
。此处 Wicket 将为您将您的登录面板添加到一个虚拟页面中。您可以断言 LoginPanel 以您需要的方式运行。
这样您就可以测试任何类型的 Wicket 组件,而不仅仅是面板。
您必须在创建 formTester 之前在 validLogin() 中设置起始页:
@Test
public void validLogin(){
//here...
tester.startPage(HomePage.class);
FormTester form = tester.newFormTester("userForm");
form.setValue("username", "emile");
form.setValue("password", "123");
form.submit();
tester.assertRenderedPage(SuccessPage.class);
}