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:
- Java 5 or above
- Struts 2.1.8.1 (including the junit and spring integration plug-ins)
- Spring Framework 2.5.6 (including spring)
- JUnit 4.4
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:
- Little to no manual setup and configuration required for tests.
- Since the tests are transactional, there's no need to manually clean up test data. Each time the tests are run, the database returns to the state it was in before running a test.
- Easy overrides. Using a combination of annotations and helper methods provided by JUnit, Struts and Spring superclasses, you can easily modify the default behavior of tests.
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).
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
|
Wiring in Spring Configuration and Setting DataSource
From here, extend the StrutsSpringTransactionalTests class:
1234567891011121314151617181920212223242526272829303132333435 |
|
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:
1234567891011121314151617 |
|
When the above test runs, any database operations are automagically rolled back.