Cactus
Cactus is yet another useful open source system from the Jakarta project. Its purpose is--and I quote from the Cactus web site:
Cactus is a simple test framework for unit testing server-side java code
(Servlets, EJBs, Tag Libs, Filters, ...). The intent of Cactus is to lower the
cost of writing tests for server-side code. It uses JUnit
and extends it. Cactus implements an in-container strategy.
So STC uses Cactus by building on its servlet features, more specifically
the ServletTestCase class.
You may ask: why offer two testing techniques--the mock object approach and
the in-container approach--if one is enough? The answer is: both strategies have
their pros and cons. As I see it, the primary advantage by using the mock
approach is its simplicity. As was shown in my first article it's very easy to
get the test set up ready, and you're not dependent on having a servlet
environment available. The advantage of the in-container approach is, of course, that
the test is carried out in the real servlet environment. This improves the
chances for having a production system that will run your Action classes
without errors.
It you're interested in more details on the two testing techniques then look
at this article on the
Cactus web site.
Installing Cactus
Before we can use Cactus from STC we'll have to
download
it from the Jakarta web site (look for "Release builds"). The size
is about 5Mb.You may also choose to download the war-file with
all the examples from this article. It's 1.6 Mb, but then you don't get
documentation and samples.
The download from the Jakarta site comes in two flavors, one for J2EE 1.2 and
one for J2EE 1.3. Choose the one that is supported by your servlet container.
I'll be using Tomcat version 4 in my examples, and it supports J2EE 1.3. Cactus
is currently on version 1.5-beta1.
The way Cactus must be installed, so STC can use it properly, is described in
the STC download file examples/README.txt. Installation can be a little
tricky, so I'll go through it step by step. If you don't have patience for this
you might simply use the war-file: drop it in your web container, restart the
web server and you're ready for running the tests but some of us like to dig down into the details, so let's do just that.
First of all you should realize that Cactus uses the same client-server set-up as when
you use a browser against a web server. This means that you'll have to consider
the classpath for the client as well as the web server. A key thing to
understand is that your test program must be on the client side as well as the
server side:
Figure 1: The Cactus architecture
The client copy of MyTestCase, the STC/Cactus/JUnit test program, issues an
HTTP request to the web server, where it's handled by a Cactus servlet (The
Redirector Proxy), which then invokes the server copy of MyTestCase. It's on the
server side that setUp and testXXX methods are called. A common mistake is
to make corrections to MyTestCase and forget to deploy it
twice!
To simplify the set up, I'll only use one copy of the test program. As the client, I use the server copy in the WEB-INF/classes
directory on my Tomcat server. The client may then also share the jar-files from the
servers WEB-INF/lib directory. If you're interested in
all the details of what jar-files are needed by the client and which ones by the
web server, then look at
the
Cactus documentation on this subject.
Step 1: Make a copy of the "DVD library" application from my
first article: copy the dvdlib directory and call it "cactusdvdlib".
Step 2: Copy three jar-files from the Cactus download's lib
directory to the cactusdvdlib/WEB-INF/lib directory. The files are:
cactus.jar
commons-httpclient.jar
aspectjrt.jar
(I've shortened the file names, which also contain release numbers).
Warning: In the Cactus documentation for installation on Tomcat,
it's recommended to put the jar files in the Tomcat's common library: common/lib.
If you do this then be prepared for strange classloader problems--e.g. missing
classes, even when they seem to be in the classpath. I've not been able to use common/lib
with Tomcat 4.1.12 for the examples in this article.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|