Chapter 7. Cactus


Cactus, available from, is an open source unit-testing framework for server side Java code. Specifically, Cactus allows you to test servlets, JSPs, and servlet filters.[31]

Cactus extends JUnit to provide three specific junit.framework.TestCase subclasses:


Each Cactus test case provides a specific function and is discussed in more detail in the following recipes. Cactus tests execute on both client and server. This is a significant departure from other testing frameworks and deserves some explanation. When using Cactus, you create a single subclass of one of the previously mentioned classes. Cactus then creates and runs two instances of your test case. One instance runs on the client JVM and the other runs inside of the servlet container’s JVM. The client side allows HTTP headers and HTTP parameters to be added to the outgoing request. The server side invokes your servlet’s methods, performs any necessary assertions, and sends back a response to the client. The client may then assert that the response contained the expected information.


It is important to know that you have to deploy your Cactus tests to the server. Specifically, you must create a web-application WAR file containing a valid web.xml file, all Cactus tests, and all support classes needed for your tests to execute. This is necessary because Cactus tests are ...

Get Java Extreme Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.