Wicket Junit测试案例

时间:2012-12-24 09:51:30

标签: java testing junit wicket

在我的wicket应用程序中,我想检查我的主页(HomePage.java)是否正确呈现。

我写了一个测试用例

public class TestMerchantProductPage extends TestCase {
private WicketTester tester = null;

@BeforeClass
public void testSetUp(){
    tester = new WicketTester();
    tester.startPage(HomePage.class);
}

@SuppressWarnings("deprecation" )
@Test
public void testPage(){
    tester.assertRenderedPage(HomePage.class);
    tester.startPanel(ItemOrderPanelBySearch.class);

}
}

我收到这样的错误

org.apache.wicket.WicketRuntimeException: Can't instantiate page using constructor 'public com.zipgrocery.pages.HomePage(org.apache.wicket.request.mapper.parameter.PageParameters)' and argument ''. Might be it doesn't exist, may be it is not visible (public).
at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:196)
at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:77)
at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:47)
at org.apache.wicket.DefaultMapperContext.newPageInstance(DefaultMapperContext.java:103)
at org.apache.wicket.request.handler.PageProvider.resolvePageInstance(PageProvider.java:264)
at org.apache.wicket.request.handler.PageProvider.getPageInstance(PageProvider.java:165)
at org.apache.wicket.util.tester.BaseWicketTester$LastPageRecordingPageRendererProvider.get(BaseWicketTester.java:2682)
at org.apache.wicket.util.tester.BaseWicketTester$LastPageRecordingPageRendererProvider.get(BaseWicketTester.java:2669)
at org.apache.wicket.request.handler.RenderPageRequestHandler.respond(RenderPageRequestHandler.java:166)
at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:784)
at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:255)
at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:212)
at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:283)
at org.apache.wicket.util.tester.BaseWicketTester.processRequest(BaseWicketTester.java:645)
at org.apache.wicket.util.tester.BaseWicketTester.processRequest(BaseWicketTester.java:565)
at org.apache.wicket.util.tester.BaseWicketTester.processRequest(BaseWicketTester.java:537)
at org.apache.wicket.util.tester.BaseWicketTester.startPage(BaseWicketTester.java:1209)
at org.apache.wicket.util.tester.BaseWicketTester.startPage(BaseWicketTester.java:1182)
at com.zipgrocery.TestMerchantProductPage.testSetUp(TestMerchantProductPage.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:173)
... 38 more
Caused by: java.lang.ClassCastException: org.apache.wicket.protocol.http.WebSession cannot be cast to com.zipgrocery.ZipSession
at com.zipgrocery.ZipSession.get(ZipSession.java:45)
at com.zipgrocery.pages.HomePage.<init>(HomePage.java:65)
... 43 more

我的HomePage.java中没有默认构造函数。那我该怎么写我的测试用例呢?

感谢任何帮助或建议。提前谢谢!

2 个答案:

答案 0 :(得分:1)

从Wicket 1.5开始,您只需使用参数调用构造函数,因此在您的情况下

WicketTester tester = new WicketTester(YourApplication);
tester.startPage(new HomePage(YourParameter));
tester.assertRenderedPage(HomePage.class);

答案 1 :(得分:1)