Writing a Cactus Test
Problem
You want to use Cactus to test server-side code.
Solution
Extend the appropriate Cactus test-case class and implement one
or more testXXX( )
,
beginXXX( )
, and endXXX( )
methods.
Discussion
Cactus is a testing framework that extends JUnit to provide a way to execute tests against code running in a server. Specifically, Cactus allows for testing servlets, JSPs, and filters while running within a servlet container.
There are seven main steps to writing a Cactus test.
Import the JUnit and Cactus classes:
import org.apache.cactus.*; import junit.framework.*;
Extend one of three Cactus test case classes:
-
org.apache.cactus.ServletTestCase
Extend this class when you want to write unit tests for your servlets. For example, if you need to test how a servlet handles
HttpServletRequest
,HttpServletResponse
,HttpSession
,ServletContext
, orServletConfig
objects, write aServletTestCase
:public class TestMyServlet extends ServletTestCase { }
-
org.apache.cactus.JspTestCase
Extend this class when you want to write unit tests for your JSPs. For example, if you need to test a custom tag library or
JspWriter
, write aJspTestCase
:public class TestMyJsp extends JspTestCase { }
-
org.apache.cactus.FilterTestCase
Extend this class when you want to write unit tests for your filters. For example, if you need to test that a
FilterChain
orFilterConfig
FilterTestCase
:public class TestMyFilter extends FilterTestCase { }
-
Implement the JUnit
setUp( )
and ...
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.