The recommended way to test actions is to instantiate the action classes and test them. The JUnit Plugin supports testing actions within a Struts invocation, meaning that a full request is simulated, and the output of the action can be tested.

Struts actions (without Spring)

To test actions that do not use Spring, extend StrutsTestCase. The following example shows different ways of testing an action:

    <constant name="struts.objectFactory" value="spring"/>
    <package name="test" namespace="/test" extends="struts-default">
        <action name="testAction" class="org.apache.struts2.TestAction">
            <result type="freemarker">/template.ftl</result>


public class TestAction extends ActionSupport {
    private String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;


package org.apache.struts2;

import org.apache.struts2.dispatcher.mapper.ActionMapping;

import java.util.HashMap;

import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.Action;

import javax.servlet.ServletException;

public class StrutsTestCaseTest extends StrutsTestCase {
    public void testGetActionMapping() {
        ActionMapping mapping = getActionMapping("/test/testAction.action");
        assertEquals("/test", mapping.getNamespace());
        assertEquals("testAction", mapping.getName());

    public void testGetActionProxy() throws Exception {
        //set parameters before calling getActionProxy
        request.setParameter("name", "FD");
        ActionProxy proxy = getActionProxy("/test/testAction.action");

        TestAction action = (TestAction) proxy.getAction();

        String result = proxy.execute();
        assertEquals(Action.SUCCESS, result);
        assertEquals("FD", action.getName());

    public void testExecuteAction() throws ServletException, UnsupportedEncodingException {
        String output = executeAction("/test/testAction.action");
        assertEquals("Hello", output);

    public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException {
        request.setParameter("name", "FD");
        String name = (String) findValueAfterExecute("name");
        assertEquals("FD", name);

The template

If you use JSPs as the template engine you won't be able to test the action output outside the container. The Embedded JSP Plugin can be used to overcome this limitation and be able to use JSPs from the classpath and outside the container.

There are several utility methods and mock objects defined in StrutsTestCase which can be used to facilitate the testing:

Method Name



Pass the url for the action, and it will return the output of the action. This output is not the action result, like "success", but what would be written to the result stream. To use this the actions must be using a result type that can be read from the classpath, like FreeMarker, velocity, etc (if you are using the experimental Embedded JSP Plugin, you can use JSPs also)


Builds an action proxy that can be used to invoke an action, by calling execute() on the returned proxy object. The return value of execute() is the action result, like "success"


Gets an ActionMapping for the url


Injects Struts dependencies into an object (dependencies are marked with Inject)


Finds an object in the value stack, after an action has been executed


Can be overwritten in subclass to provide additional params and settings used during action invocation

createAction(Class)Can be used to instantiate an action which requires framework's dependencies to be injected (e.g. extending ActionSupport requires inject some internal dependencies)



MockHttpServletRequest request

The request that will be passed to Struts. Make sure to set parameters in this object before calling methods like getActionProxy

MockHttpServletResponse response

The response object passed to Struts, you can use this class to test the output, response headers, etc

MockServletContext servletContext

The servlet context object passed to Struts

Struts Actions using Spring

Make sure to add a dependency to the Spring Plugin to your pom.xml:


If you use Spring as the object factory, the StrutsSpringTestCase class can be used to write your JUnits. This class extends StrutsTestCase and has a applicationContext field of type ApplicationContext.

The Spring context is loaded from "classpath*:applicationContext.xml" by default. To provide a different location, overwrite getContextLocations.

  • No labels


  1. Anonymous

    Is the JUnit example code missing the declaration of some instance fields (for example request)? Even after extending StrutsTestCase I'm getting an error that request cannot be resolved.

    1. Anonymous

      I'm replying to my own comment above. To use the StrutsTestCase class you have to checkout the latest plugin code from the subversion repository (

    2. Anonymous

      I have the same problem
      how do you fix this problem?

  2. Anonymous

    First of all thanks for you job. There is a problem using this plugin and the result type tiles: after proxy.execute() a NullPointerExcepion is thrown at org.apache.struts2.views.tiles.TilesResult.doExecute(, probably cause it cannot load the StrutsTilesListener from web.xml.
    Any suggestions?

  3. Anonymous

    I am attempting to write a unit test where my Actions are configured using the convention plugin (ie with annotations NO struts.xml). I keep on getting the following exception:

    Unable to load configuration. - bean - jar:file:/Users/username/.m2/repository/org/apache/struts/struts2-core/2.3.12/struts2-core-2.3.12.jar!/struts-default.xml:29:72
    at org.apache.struts2.dispatcher.Dispatcher.init(
    at org.apache.struts2.util.StrutsTestCaseHelper.initDispatcher(
    at org.apache.struts2.StrutsTestCase.initDispatcher(
    at org.apache.struts2.StrutsTestCase.setUp(
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
    at com.intellij.rt.execution.junit.JUnitStarter.main(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at com.intellij.rt.execution.application.AppMain.main(