This document is a step by step tutorial that explains how to set up Cactus and run Cactus tests in Tomcat in less than 10 minutes ! (discounting download time of course :-)).
There are 2 ways of packaging Cactus so that you can execute Cactus tests on your application:
WEB-INF/lib
directory,
as described in the Classpath
Tutorial,
[tomcat-root]
.
Download the Cactus jars from the Cactus
download page. They are located in the lib/
directory in the zip.
Copy the following jars to [tomcat-root]/common/lib
:
cactus.jar
commons-httpclient.jar
commons-logging.jar
junit.jar
aspectjrt.jar
httpunit.jar
.
[tomcat-root]/conf/web.xml
and add the following at
the beginning of the file, after the <webapp>
tag:
<servlet> <servlet-name>ServletRedirector</servlet-name> <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class> <init-param> <param-name>param1</param-name> <param-value>value1 used for testing</param-value> </init-param> </servlet> <servlet> <servlet-name>ServletTestRunner</servlet-name> <servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class> </servlet>
<servlet>
definition (there
are a few provided by Tomcat in addition to our 2 above), add:
<servlet-mapping> <servlet-name>ServletRedirector</servlet-name> <url-pattern>/ServletRedirector</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ServletTestRunner</servlet-name> <url-pattern>/ServletTestRunner</url-pattern> </servlet-mapping>
web.xml
file. If later on you wish to use
the Cactus Ant integration and more specifically if you use the
<cactifywar>
Ant task, you may run into problems.
The <cactifywar>
task automatically adds the
needed Cactus redirectors (thus they'll be added twice leading to
an error.
[tomcat-root]/webapps |_ test |_ WEB-INF |_ classes
SampleServlet.java
java source
file, compile it and copy the resulting .class file in
[tomcat-root]/webapps/test/WEB-INF/classes
.
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; public class SampleServlet extends HttpServlet { public void saveToSession(HttpServletRequest request) { String testparam = request.getParameter("testparam"); request.getSession().setAttribute("testAttribute", testparam); } }
We're now read to create our first Cactus test case. Create the
following TestSampleServlet.java
java source file, compile
it and copy the resulting .class file in
[tomcat-root]/webapps/test/WEB-INF/classes
.
import junit.framework.Test; import junit.framework.TestSuite; import org.apache.cactus.ServletTestCase; import org.apache.cactus.WebRequest; public class TestSampleServlet extends ServletTestCase { public TestSampleServlet(String theName) { super(theName); } public static Test suite() { return new TestSuite(TestSampleServlet.class); } public void beginSaveToSessionOK(WebRequest webRequest) { webRequest.addParameter("testparam", "it works!"); } public void testSaveToSessionOK() { SampleServlet servlet = new SampleServlet(); servlet.saveToSession(request); assertEquals("it works!", session.getAttribute("testAttribute")); } }
Time to enjoy our hard work ! Start Tomcat by running
[tomcat-root]/bin/startup.bat
(for windows) or
[tomcat-root]/bin/startup.sh
(for unix).
Open a browser and point it at
http://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet
You should see:
Ok, that's nice ... But what if I want HTML instead of XML? Don't
worry there is a solution. Grab the following
XSLT stylesheet
(based on the stylesheet used by the
<junitreport> Ant task) and drop it in
[tomcat-root]/webapps/test
. Then, open a browser and type
http://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet&xsl=cactus-report.xsl
.
You should now see the following: