At Webify, we have three main subsystems of our applications: Hibernate for the database layer, Spring for the component container layer and Tapestry for the Web UI layer.
It’s relatively easy to unit test Hibernate and Spring code via JUnit, they both provide hooks so you can start them up by hand. Tapestry, on the other hand, is meant to run within an application server - it requires a container to run. Well, mock objects have recently become popular as a way of testing code which requires a container to run; you just create the container objects which are used by this code so that it thinks it is running within a container:
MockServletContext servCtx = new MockServletContext("/webroot");
MockServletConfig servCfg = new MockServletConfig(servCtx, "tapestry");
ApplicationServlet servlet = new ApplicationServlet();
servlet.init(servCfg);
MockHttpServletRequest req = new MockHttpServletRequest(servCtx, "GET", "/");
MockHttpServletResponse resp = new MockHttpServletResponse();
servlet.service(req, resp);
if (!pageRequested) {
fail("Home page was not executed");
}
The ApplicationServlet is the method of entry into the Tapestry system. On the filesystem, you create a “tapestry.application” file in webroot/WEB-INF which contains the page definitions. In this case, this just tests that the Home page is rendered.
public class HomePage extends BasePage implements PageRenderListener {
public void pageBeginRender(PageEvent arg0) {
RequestTest.setPageRequested();
}
}
Pretty simple, huh? Now we can do basic login testing as part of our unit tests to ensure that the Web UI actually works, rather than needing to run a container and testing it by hand!
3 responses so far ↓
1 Yo Daddy // Feb 13, 2005 at 12:39 pm
Uh…… ok.
2 Raj // Feb 17, 2005 at 10:11 am
Oh, this is useful. I had no idea mock objects were “popular” these days ..
3 Herm // Nov 17, 2005 at 10:25 am
Really nice but how would you know what to put as parameters in your request? Tapestry hides the servlet api and you don’t have control over the parameters’ names as they are generated on-the-fly.
This really bugs me because I would want to use Tapestry at work, but the inability to make simple (but valuable) unit tests is something my bosses won’t allow. Tapestry Test Assist won’t be enough to convince them (and me neither).