Why Cactus?

Cactus is an open source framework that provides in-container testing services for servlets, JSP custom tags, and servlet filters. Other container components, such as connection pools or EJBs, can also be easily tested through Cactus. Cactus works by providing objects called "redirectors" that serve as points of entry to the container. These redirectors execute test cases written for Cactus in the container and provide access to container objects such as HttpServletRequest, PageContext, and FilterChain. There is one proxy per directly supported API: one for servlets, one for filters, and one for custom tags. More are planned for the future. Developers who wish to work with components that are indirectly supported can choose the most appropriate redirector (usually ServletRedirector) to gain access to the container. In addition to providing an entry into the container and access to the implicit objects, Cactus integrates with the JUnit framework and makes the business of talking to the redirector transparent to the user (well, almost transparent). Writing Cactus test cases can be as easy as extending a Cactus base class and using the objects that Cactus provides.

Java Start Sidebar
Cactus vs. HttpUnit

Cactus and HttpUnit both test Web app components, so questions naturally arise about their relationship and respective domains. Although there is some overlap, the basic difference is that Cactus is the more unit-oriented of the two. Cactus tests are designed to exercise the behaviors of specific classes and methods, whereas HttpUnit is designed to exercise requests to specific resources on a server. A Cactus test might target the behavior of HelloWorldServlet, whereas an HttpUnit test might target Depending on the server configuration, this path could invoke several filters and/or kick the user out to a login page before allowing access to HelloWorldServlet. Cactus tests pieces of the chain of objects that generate a response, and HttpUnit tests the whole chain. Although each tool has its separate place (Cactus for unit testing and HttpUnit for functional testing), the line sometimes blurs. For instance, a simple servlet might be in charge solely of writing some data to the response. In this case, inspection of the servlet response is all the testing the servlet needs. HttpUnit, Cactus, or a combination of the two would serve.

HttpUnit ships with an interesting companion API called ServletUnit. This framework essentially provides a stub container for servlets to reside in and allows these servlets to be accessed as if they were running in a remote Web server. Although ServletUnit could be useful, it still leans toward the functional end of testing--its domain does not overlap that of Cactus.

Java End Sidebar