2013-02-10 20 views
3

使用wicketframework可以使用wickettester並查看lastrenderedpage。 我想測試我的wicketapplication是否將用戶重定向到我的自定義ErrorPage runtimeexceptions和缺少資源(404)。Wicket(Java):如何使用wickettester來測試404是否重定向到正確頁面

對於第一種情況,我將重定向設置爲我的自定義錯誤頁面,並驗證它的工作原理。基本上, 「settings.setInternalErrorPage(ErrorPage.class);」 並且具有單元測試,基本上雲:

tester = new WicketTester(webApplication); 
     APage aPage = new APage(); 
     tester.startPage(aPage); 
     tester.assertRenderedPage(APage.class); 
     tester.setExposeExceptions(false); 

     //Page rendered ok, now throw an exception at a button submit 
     when(aPage.getServiceFacade()).thenThrow(new RuntimeException("DummyException")); 
     tester.submitForm("searchPanel:searchForm"); 
     tester.assertRenderedPage(ErrorPage.class);//YES, we got redirected to deploymode 

所以測試runtimeexecptions - >的errorPage工作正常。 現在來測試缺少的資源。 基本上我在測試時建立的web.xml與「

<error-page> 
    <error-code>404</error-code> 
    <location>/error404</location> 
    </error-page> 

,然後我也裝在檢票口。 這工作正常實際使用。但是...我想這..

MockHttpServletRequest request = tester.getRequest(); 
     String contextPath = request.getContextPath(); 
     String filterPrefix = request.getFilterPrefix(); 
     tester.setExposeExceptions(false); 
     tester.executeUrl(contextPath + "/" + filterPrefix + "/"+ "startpage" + "/MyStrangeurlToNothing); 
     tester.assertRenderedPage(ErrorPage.class); 

但是,測試者對象上的lastrenderedpage在這種情況下不起作用,並且給我null。我想WicketTester不會讀取web.xml文件,所以不知道如何映射這個錯誤。要測試這個嗎?

+0

嗨你有沒有得到任何與此有關的最終?我有同樣的問題 – 2014-01-31 09:22:38

回答

4

我知道這已經很舊了,但j烏斯季萬一有人有同樣的問題(像我一樣):

我發現這裏的解決方案:

https://issues.apache.org/jira/browse/WICKET-4104

這在我的情況下翻譯成:

WicketTester tester = ...; // set up tester 
tester.setFollowRedirects(false); // important 
tester.startpage(...); 
// or 
tester.executeUrl(...); 

// then test to see if there was a redirect 
assert tester.getLastResponse().getRedirectLocation() != null; 
assert aTester.getLastResponse().getRedirectLocation().endsWith("...."); 

// then manually redirect to that page and test its the page you expected 
Url url = Url.parse(aTester.getLastResponse().getRedirectLocation()); 
aTester.executeUrl(url.getPath().substring(1)); // the substring chops off and leading/
aTester.assertRenderedPage(YourRedirectPage.class); 

希望幫助有人

相關問題