advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : StrutsTestCase: The Tool for Struts Unit testing :

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. 

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs