Title: Testing Harness

Hi All,

I've just checked in the first release of the Wicket testing harness. This harness allows you to create and execute Wicket applications within a unit test environment. In particular you can:

  • Quickly get the minimum Wicket environment up and running so that it is possible to unit test things like page/component creation, localization and so on.
  • Set up a request to Wicket, execute the full render cycle and then unit test both the changes to the components/models and the produced HTML
  • Easily activate links, form submits, redirects, and bookmarkable pages

All of the code for this is in the src/test tree in the package com.voicetribe.wicket.protocol.http. (It needed to be here in order to access some package level methods that Jonathan had created in the Wicket core).

Within this package is also a small test example made up of the files MockHttpApplicationTest.java, MockPage.java and MockPage.html. This actually tests the test harness and also provides a worked example of the main features.

And now a quick overview......

To get Wicket into a stable state for doing tests that rely on Pages, Components, Localization and so on all you need to do within your unit test code is create a new MockHttpApplication instance:

        MockHttpApplication application = new MockHttpApplication(null);


To access the home page of an application you can call:

        application.getSettings().setHomePage(MockPage.class);
        application.setupRequestAndResponse();
        application.processRequestCycle();

This initialises and executes a full request cycle call through Wicket.


If you want to call a specific component, run a redirect or populate a form then you can use one of the request convenience methods like:

        MockPage page = (MockPage)application.getLastRenderedPage();
        application.setupRequestAndResponse();
        application.getServletRequest().setRequestToComponent(page.get("actionLink"));
        application.processRequestCycle();


Once a request cycle has completed you can obtain the Page and unit test the state of the components:

        MockPage page = (MockPage)application.getLastRenderedPage();
        Label serverIP = (Label)page.get("serverIP");
        Assert.assertEquals("127.0.0.1", serverIP.getModel().getObject());

You can also use the classes in the documentvalidation sub package to build an in-memory model of the HTML elements and text that the returned HTML document should contain and then validate the document against these (see the example for full details). This is actually something that could be really useful for testing that components render themselves correctly (as in the example) and should allow us to build unit tests for every core Wicket component.


As far as I am aware the test harness should fully emulate a single-threaded, single-user Servlet Engine for all of the main methods in javax.servlet and javax.servlet.http. The only things that I have not implemented any support for at the moment are:

  • Accessing resources other than those accessible via the Wicket application (e.g. resources that are not tied to components)
  • Request dispatching
  • Multi-part or binary form data (e.g. upload)

I can add these in at a future date if people feel they are important or we need to runs tests that require this functionality.

Regards,
Chris

       

Reply via email to