[ Team LiB ] |
7.1 IntroductionCactus, 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.[1]
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. 7.1.1 Implicit ObjectsEach Cactus test case has a set of implicit objects. Implicit objects are only valid on the test case instance running in the server. These objects are used to set up information that a servlet expects to exist before invoking any methods to test. For example, you can use the config implicit object to set up initialization parameters. Here are the implicit objects defined by each test case: org.apache.cactus.ServletTestCase HttpServletRequestWrapper request HttpServletResponse response HttpSession session ServletConfigWrapper config org.apache.cactus.JspTestCase PageContextWrapper pageContext JspWriter out org.apache.cactus.FilterTestCase HttpServletRequestWrapper request HttpServletResponse response FilterConfigWrapper config FilterChain filterChain 7.1.2 How Does It Work?Cactus executes your tests on the client and server. This means two instances of your test case are created to run the tests. Figure 7-1 shows the execution of a Cactus test. Figure 7-1. Execution of a Cactus testFirst, the JUnit test runner, executing in the client JVM, creates one instance of your test case. A redirector proxy executing in the server JVM creates the second instance. A redirector proxy is responsible for managing the server-side execution of a test. Let's walk through an example:
|
[ Team LiB ] |