Wednesday June 22, 2005
Plant Cactus in NetBeans IDE 4.1
Cactus is the current cool thing, everywhere you go you hear about it. Well, anyway, everywhere I go, which doesn't say all that much since I only leave the building to teach English. Nonetheless, according to a JavaOne presentation I've been looking through, it's "a simple test framework for unit testing server-side Java code (Servlets, EJBS, Tag Libs, Filters,...)". I've been working through the process of setting Cactus up in NetBeans, and using it over Tomcat, so that I can find out what all the buzz is about.
So, as a quick preview, here's what I ended up with at the end of the procedure:

And here's a brief run-down of the procedure for testing servlets (it assumes you've already got a web application):
- Get the Cactus software. Download it here. Then, once you've unpacked and unzipped everything, do the following to set everything up:
- Make Cactus JARs available to Tomcat. Copy the Cactus JARs into Tomcat's common/lib folder. (Don't worry about servletapi.jar though.)
- Make Cactus JARs available to your IDE project. Add the JARs you copied to Tomcat's common/lib to your application's Compile Test Classpath. (Right-click the project, choose Properties, click Libraries, and add them to the Compile Tests tab).
- Make the Cactus report formatter available to your IDE project. Go to http://jakarta.apache.org/cactus/misc/ and download cactus-reports.xsl. Put it in your application's test folder.
- Modify web.xml. Add servlet definitions and mappings in web.xml for ServletRedirector and ServletTestRunner:
<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-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> - Copy or create Cactus test classes. Copy classes and their related test classes from the Cactus distribution to your own application's folders. For example, get SampleServlet.java from samples\servlet\src\java\org\apache\cactus\sample\servlet and put it in a package in your Source Packages and then copy TestSampleServlet.java from samples\servlet\src\test-cactus\org\apache\cactus\sample\servlet to a package in your application's Test Packages folder.
Alternatively, assuming you know how to do it, just create your own Cactus test classes.
- Modify your Ant build file. Now you'll need to add the following to your build.xml Ant script:
<target name="-post-compile-test" depends="run"> <copy todir="${build.web.dir.real}" file="${test.src.dir}/org/me/hello/cactus-report.xsl" /> <copy todir="${build.classes.dir.real}"> <fileset dir="${build.test.classes.dir}" excludes="**/*.java"/> </copy> <input message="Which file do you want to run?" validargs="TestSampleServlet,TestSampleServletConfig" addproperty="testfile"/> <antcall target="run-cactus-test" /> </target><target name="run-cactus-test" depends="run-deploy,run-display-test"/>
<target name="run-display-test" depends="run-deploy"> <nbbrowse url="http://localhost:8084/CactusTest/ServletTestRunner?suite=org.me.hello.${testfile}&xsl=cactus-report.xsl"/> </target>
Now, when you select the project's node in the Projects window, and then choose Run > Test, you're prompted to select the class that you'd like to run. When you do this, the testfile property is set, specifying the test class that Cactus will run. And then a nice report is produced, using the cactus-report.xsl file, which gives it a friendly structure (click to enlarge):
Jun 22 2005, 04:27:04 AM PDT Permalink



