O'Reilly logo

Java Extreme Programming Cookbook by Brian M. Coyner, Eric M. Burke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

  1. Import the JUnit and Cactus classes:

    import org.apache.cactus.*;
    import junit.framework.*;
  2. 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, or ServletConfig objects, write a ServletTestCase:

    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 a JspTestCase:

    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 or FilterConfig object executes correctly, write a FilterTestCase:

    public class TestMyFilter extends FilterTestCase {
    }
  3. Implement the JUnit setUp( ) and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required