为什么我的单元测试在尝试渲染另一个页面时给我一个AssertionFailedError

问题描述 投票:0回答:1

我正在学习单元测试,并尝试检查登录名在HomePage上是否有效,并且是否可以显示SuccessPage,但它给了我AssertionFailedError

。java:


    @Test
    public void validLogin(){
        tester.startPage(HomePage.class);

        FormTester form = tester.newFormTester("userForm");
        form.setValue("username", "emile");
        form.setValue("password", "123");
        form.submit();
        //onsubmit the Successpage is rendered in response
        tester.assertRenderedPage(SuccessPage.class);

    }

错误:

[main] INFO org.apache.wicket.Application - [WicketTesterApplication-768a67f4-8bf1-45d0-bdcb-3a9be18172e1] init: Wicket core library initializer
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-768a67f4-8bf1-45d0-bdcb-3a9be18172e1] init: Wicket extensions initializer
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-768a67f4-8bf1-45d0-bdcb-3a9be18172e1] init: Wicket jQuery UI initializer
[main] INFO org.apache.wicket.Application - [WicketTesterApplication-768a67f4-8bf1-45d0-bdcb-3a9be18172e1] init: Wicket jQuery UI initializer (theme-uilightness)
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

junit.framework.AssertionFailedError: classes not the same, expected 'class com.mycompany.SuccessPage', current 'class com.mycompany.HomePage'

    at org.apache.wicket.util.tester.WicketTester.assertResult(WicketTester.java:798)
    at org.apache.wicket.util.tester.WicketTester.assertRenderedPage(WicketTester.java:697)
    at com.mycompany.TestHomePage.validLogin(TestHomePage.java:48)
    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$1.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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)
wicket wicket-tester
1个回答
0
投票

我得到了答案,所以我没有将form.submit();引用到onSubmit()函数,所以我基本上是这样做的:

@Test
    public void validLogin() throws Exception {
        tester.startPage(HomePage.class);

    //  tester.startComponentInPage(HomePage.class);
        FormTester form = tester.newFormTester("userForm");
        form.setValue("username", "emile");
        form.setValue("password", "123");
        form.submit("error"); // new code that was edited
        //onsubmit the Successpage is rendered in response
        tester.assertRenderedPage(SuccessPage.class);


    }

form.submit("error");现在引用了wicket:id="error",并通过了测试:D

© www.soinside.com 2019 - 2024. All rights reserved.