Wicket (Java): how to use a wickletter to check if a 404 redirect to the correct page

With wicketframework, you can use the wickletter and view the lastrenderedpage. I want to test if my wicketapplication redirects the user to my custom ErrorPage runtimeexceptions and missing resources (404).

In the first case, I set a redirect to my own error page and check if 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 we are testing the missing resources. Basically I created web.xml with

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

      

which is then also mounted in the wicket doors. This works great for real use. 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, the lastrenderedpage on the tester object does not work in this case and gives me null. I am assuming that WicketTester, for example, does not read web.xml and therefore did not know how to map this error. Any hints on how to check this?

+3


source to share


1 answer


I know this is very old, but just in case someone has the same question (as I did):

I found a solution here:

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



Which in my case is translated to:

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 someone can help

+4


source







All Articles