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, orServletConfigobjects, 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
FilterChainorFilterConfigobject executes correctly, write aFilterTestCase:public class TestMyFilter extends FilterTestCase { }
-
Implement the JUnit
setUp( )and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access