Reputation: 725
With the wicketframework it's possible to use wickettester and see lastrenderedpage. I want to test if my wicketapplication redirects the user to my custom ErrorPage on runtimeexceptions and missing resources (404).
For the first case i set the redirect to my custom errorpage and verified it works. Basically,
" settings.setInternalErrorPage(ErrorPage.class);
"
and having a unittest that basically goes:
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
So the test for runtimeexecptions -> errorpage works fine. Now to testing for missing resources. Basically i set up the web.xml with "
<error-page>
<error-code>404</error-code>
<location>/error404</location>
</error-page>
which i then also mounted in wicket. This works fine for real usage. But when testing.. i tried this..
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);
But, lastrenderedpage on tester object doesn't work in this case and gives me null. I guess WicketTester doesn't read the web.xml for example, and so, wouldn't know how to map this error. Any hints on how to test this?
Upvotes: 3
Views: 1718
Reputation: 1844
I know this is very old, but just in case someone has the same question (as I did):
I found the solution here:
https://issues.apache.org/jira/browse/WICKET-4104
Which in my case translated into:
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);
Hope that helps someone
Upvotes: 4