2012-12-24 249 views
2

在我的wicket應用程序中,我想檢查我的主頁(HomePage.java)是否正確呈現。Wicket Junit測試用例

我寫測試用例

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

由於檢票1.5你剛纔打電話與參數的構造,所以你的情況

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

感謝您的幫助。但我沒有得到這個exapmle。什麼是getTestPanel()做什麼,我應該在哪裏使用它? –

+0

好吧,答案很簡單... tester.startPage(new HomePage(...)); 我編輯了我的答案 – Tariq

+0

我明白了。感謝Tariq的幫助。 –