1.4.2. JUnit and Cactus: Testing Your Objects, Components, and Applications
In any application development context, the practice of testing your code is a critical
success factor. Testing enterprise applications involves a number of different dimensions.
The user experience and the proper behavior of views (in the model-view-controller, or
MVC, sense) can be tested using functional testing tools; the correct behavior of software
at the code level, in terms of objects and components performing as expected, can be tested
using unit testing tools; the throughput and overall behavior of a system when under heavy
load (large numbers of users or transactions or both) can be tested using performance
JUnit is an open source unit testing framework for Java. It is one in a series of such tools,
built for a number of development environments using the same conceptual architecture.
Other tools in this suite include PerlUnit for Perl and CppUnit for C++. JUnit includes a
Java API that provides interfaces and base classes for defining and running unit tests as
well as some tools that facilitate the configuration, running, and reporting of unit test
suites. JUnit is used by developers to write suites of unit tests that exercise their code in
critical ways and that verify the results to ensure that they are correct, according to the
documented behavior of the code under test. These unit tests are themselves Java code,
making it easy to use the same code management tools to manage tests for code along with
the code itself.
Enterprise developers face unique challenges when unit testing their Java objects and
components. The proper behavior of enterprise code can be tested only if a suitable
simulation of its runtime environment can be achieved. A web component like a JSP tag
handler, for example, can be tested only if the testing framework can operate within a web
container, can issue simulated handle requests to the tag handler, and can interpret the
responses that are generated to assess whether the test succeeded or not.
To facilitate the task of enterprise unit testing, the good people at Apache extended JUnit
with a framework called Cactus. Cactus allows J2EE developers to write unit tests for full
enterprise components, like servlets and EJBs.
Chapter 18 provides an introduction to the basic JUnit testing framework and also gives
a tutorial on using Cactus to define and execute test suites for enterprise components.