Tomcat Quickstart forewords this Guide applies to cactus1.4 and later versions, tomcat4.0 and later versions.
This document demonstrates how to install and run the cactus test on Tomcat within 10 minutes (of course, excluding the download time pai_^.
There are two ways to perform the cactus test on your application:
- Put all the cactus jar packages under your WEB-INF/lib directory. For details, see classpath tutorial,
- Put the cactus jar package in the classpath of your container, so that cactus will use the context class loader of the container to load them. This policy will be described in this Guide. This is also a method that allows many web applications to share cactus with each other with only comparative interference.
In addition, there are many ways to trigger the execution of the cactus test (see the testrunner howto guide ). In this guide, we will describe the simplest way to install cactus on a browser.
Steps 1st to 3rd are a step to ensure that cactus can run on Tomcat, which only needs to be installed once. Step 2: install Tomcat
Download tomcat4.0 or later and decompress it to a directory. We call this directory[tomcat-root]
.
Step 2: copy the cactus jar package
Download the cactus jar package from cactus download page.lib/
Directory.
Copy the following jar package[tomcat-root]/common/lib
Below:
cactus.jar
commons-httpclient.jar
commons-logging.jar
junit.jar
aspectjrt.jar
This is the minimum set of jar required to run cactus. If you want to use cactus for httpunit integration later, you also need to copy
httpunit.jar
Package. Step 2: Modify the Tomcat web. xml file
Edit[tomcat-root]/conf/web.xml
File, put the following content at the beginning of the file<webapp>
Label.
<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>
Then, in the last<servlet>
Definition (in addition to the two <servlet> above, Tomcat also provides some .) Add the following content:
<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>
Warning: Be careful when you modify the global Tomcat
web.xml
File. If later on you wish to use the cactus ant integration and more specifically if you use
<cactifywar>
Ant task, you may run into problems.
<cactifywar>
Task automatically adds the needed cactus redirectors (thus they'll be added twice leading to an error. Step 4th: create a simple application for testing
Now, we create a very simple application on the server so that we can perform unit tests on it.
First, create the following directory structure:
[tomcat-root]/webapps |_ test |_ WEB-INF |_ classes
Next, create the followingSampleServlet.java
Java source file, compile and copy the compiled. Class file[tomcat-root]/webapps/test/WEB-INF/classes
Directory.
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); }}
You'll notice that this isn' t even a finished servlet! However, this shows that you can start testing your code with Cactus even before you have finished writing it completely. Extreme programmers shold like this
We're re now read to create our first cactus test case. Create the followingTestSampleServlet.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")); }}
Step 2: run the test
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 ithttp://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet
You shoshould see:
Step 2: more interesting things!
Good, but how can I see HTML instead of XML? Don't worry. Here is a method. Grab the following XSLT stylesheet (based on the stylesheet used by<Junitreport>Ant task) and drop it in[tomcat-root]/webapps/test
. Then, open the browser and enter the following in the address bar:http://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet&xsl=cactus-report.xsl
. Now you should see the following interface:
This will work with any browser that supports client-slide XSLT transformations (both Internet Explorer and Mozilla do, for example ).