How to Test Struts 2 Actions Without a Container

Published: 25 Jan 2010

I recently worked on a project that used Struts 2.1 and the Spring framework integration plug-in. There are sporadic examples of how to test Struts actions in this context but none of them seemed to be up-to-date or they required you to start a servlet container to run the tests. None of the examples I found also showed you how to leverage Spring's transactional JUnit helper classes which enable you to run tests in a transaction and automatically rollback after each test. This is a huge benefit if you're working with a project where lots of test data is needed.

Since none of the existing examples seemed to address my needs, I put together a solution using a combination of mock objects and Spring's test helper classes.

Here's what you need:

The versions outlined above are the ones I used so I know they work. You may be able to use slightly different versions.

The approach I use here will have the following benefits:

Let's get started with some code.

Extending StrutsTestCase

I started with a base class called StrutsSpringTransactionalTests which extends the StrutsSpringTestCase. The StrutsSpringTestCase is bundled with the struts2-junit-plugin.jar and provides functionality for adding the Spring context to the servlet context (normally achieved via the web descriptor).

Wiring in Spring Configuration and Setting DataSource

From here, extend the StrutsSpringTransactionalTests class:

You can optionally combine the above two classes. I separated them in order to reuse StrutsSpringTransactionalTests across different Struts applications.

Struts Action Test Implementation

Finally, here's an example of a test class which leverages AppTransactionalStrutsTestCase:

When the above test runs, any database operations are automagically rolled back.

Related Reading