简体   繁体   中英

Wicket (Java) : How to use wickettester to test if 404 redirected to correct page

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM