|Home > FAQs > How can we test Actions|
The Struts 2 Action doesn't expose HTTP in its signature. When access to HTTP resources is needed, references to these resources can be injected at runtime, but, to the Action, the servlet resources appear as ordinary Maps. When running tests outside of the framework, the test scaffolding can create and set a Map with whatever objects are needed by the test.
There are several approaches to unit testing Struts 2-based applications, whether using JUnit or TestNG. Two popular techniques are direct Action invocation and testing interceptors and result-types independently. We will demonstrate both approaches using JUnit 3.
The simplest approach is to instantiate your Actions, call the appropriate setters, then invoke execute. Calling the Action directly allows you to bypass all the complicated container setup.
Check out the test suites in XWork and Struts 2. These suites are comprehensive and provide a good starting point. Here's an example.
The framework also provides built-in support for JUnit 3.8 via an abstract StrutsTestCase, which provides common Struts variables and setup code.