Test-Driven Development Using StrutsTestCase
by John Ferguson Smart
10/26/2005
StrutsTestCase is a
powerful and easy-to-use testing framework for Struts actions.
Using Struts and then
StrutsTestCase, in combination with traditional JUnit tests, will give you a very high
level of test coverage and increase your product reliability
accordingly.
StrutsTestCase is a test framework based on JUnit for testing
Struts actions. If you use Struts, it can provide an easy and
efficient manner for testing the Struts action classes of your
application.
Typical J2EE applications are built in layers, as illustrated in
Figure 1.
- The DAO layer encapsulates database access. Hibernate mapping
and object classes, Hibernate queries, entity EJBs, or some other
entity-relation persistence technology may be found here.
- The business layer contains more high-level business services.
Ideally, the business layer will be relatively independent of the
database implementation. Session EJBs are often used in this
layer.
- The presentation layer involves displaying application data for
the user and interpreting the user requests. In a Struts
application, this layer typically uses JSP/JSTL pages to display
data and Struts actions to interpret the user queries.
- The client layer is basically the web browser running on the
user's machine. Client-side logic (for example, JavaScript) is sometimes
placed here, though it is hard to test efficiently.

Figure 1. Typical J2EE architecture
The DAO and business layers can be tested either using classic
JUnit tests or some of the various JUnit extensions, depending
on the architectural details. DbUnit is a good choice for
database unit testing--see Andrew Glover's "Effective
Unit Testing with DbUnit" for more on DbUnit.
On the other hand, testing Struts actions has always been
difficult. Even when business logic is well confined to the
business layer, Struts actions generally contain important data
validation, conversion, and flow control code. Not testing the
Struts actions leaves a nasty gap in code coverage. StrutsTestCase
lets you fill this gap.
Unit testing the action layer also provides other benefits:
- The view and control layers tend to be better thought out and
are often simpler and clearer.
- Refactoring the action classes is easier.
- It helps to avoid redundant and unused action classes.
- The test cases help document the action layer, which can help
when writing the JSP screens.
These are typical benefits of test-driven development, and they are
as applicable in the Struts action layer as anywhere else.
Introducing StrutsTestCase
The StrutsTestCase project
provides a flexible and convenient way to test Struts actions from
within the JUnit framework. It lets you do white-box testing on
your Struts actions by setting up request parameters and checking
the resulting Request or Session state after the action has been
called.
StrutsTestCase allows either a mock-testing approach, where the
framework simulates the web server container, or an in-container
approach, where the Cactus framework
is used to run the tests from within the server container (for
example, Tomcat). In general, I prefer the mock-testing approach
because it is more lightweight and runs faster, and thus allows a
tighter development cycle.
All StrutsTestCase unit test classes are derived from either
MockStrutsTestCase for mock testing, or from
CactusStrutsTestCase for in-container tests. We'll
concentrate on mock testing here, as it requires less setup and is
faster to run.