Chapter 7. Cactus

Introduction

Cactus, available from http://jakarta.apache.org/cactus, 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:

org.apache.cactus.ServletTestCase
org.apache.cactus.JspTestCase
org.apache.cactus.FilterTestCase

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.

Tip

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.