2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Forewords and Requirements

This page explains how to run Cactus tests with the Jetty servlet container. You will need the single Jetty jar, and if you are using JSPs you will also need the Jasper jars.

Setting up Jetty

The idea is to use the org.apache.cactus.extension.jetty.JettyTestSetup JUnit TestSetup class provided by Cactus so that Jetty is started before the tests are run.

In order to use the TestSetup, you need to wrap your test suite in JettyTestSetup:

public class TestJettyAll extends TestCase
{
    public static Test suite()
    {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(MyTestClass1.class);
        [...]
        suite.addTestSuite(MyTestClassN.class);
        
        return new JettyTestSetup(suite);
    }        

You can configure the Jetty TestSetup either programatically or with system properties:

  • The method setConfigFile(File) or the system property cactus.jetty.config sets the location of an optional jetty.xml file (see Jetty's documentation for details).
  • The method setResourceDir(File) or the system property cactus.jetty.resourceDir sets the directory containing the web-application resources (expanded WAR). If you omit this step, you'll only be able to use the Servlet Redirector.
In both cases, the value set programatically takes precendence over the value of the corresponding system property. If you're using JSPs you'll need to add the Jasper jars to your runtime Classpath (in addition to the Jetty jar).

Example 1: Running from Ant

Let's assume we have a JUnit task that launches the tests. We need to add the Jetty jar and optionally the Jasper jars (if we use JSPs) to the runtime classpath. Then, all we have to do is add the correct VM arguments to configure the JettyTestSetup behavior. For example:
<junit>

    [...]
    
    <jvmarg value="-Dcactus.contextURL=http://localhost:${test.port}/test"/>
    <jvmarg value="-Dcactus.jetty.resourceDir=${src.webapp.dir}"/>
    <jvmarg value="-Dcactus.jetty.config=${src.webapp.dir}/test/jetty.xml"/>

    <classpath>
        [...]
        <!-- Include Jetty jar and optionally Jasper jars in the classpath -->
        <pathelement location="${jetty.jar}"/>
        <pathelement location="${jasper-compiler.jar}"/>
        <pathelement location="${jasper-runtime.jar}"/>    
    </classpath>
    
    <test name="org.apache.cactus.sample.TestJettyAll"/>

</junit>

Example 2: Running from an IDE

Again all you have to do is add the correct VM arguments, and the Jetty and Jasper jars, to the JUnit launch.

For example in Eclipse you would add the jars, and create a JUnit launch configuration as below:

Eclipse configuration for Jetty