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.

Direct Action Invocation

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.

Taken from Petsoar
package org.petsoar.actions.inventory;

import com.mockobjects.constraint.IsEqual;
import com.mockobjects.dynamic.C;
import com.mockobjects.dynamic.Mock;
import com.opensymphony.xwork.Action;
import junit.framework.TestCase;
import org.petsoar.pets.Pet;
import org.petsoar.pets.PetStore;

public class TestViewPet extends TestCase {
    private Mock mockPetStore;
    private ViewPet action;

    protected void setUp() throws Exception {
        mockPetStore = new Mock(PetStore.class);
        PetStore petStore = (PetStore) mockPetStore.proxy();

        action = new ViewPet();

    public void testViewPet() throws Exception {
        Pet existingPet = new Pet();

        Pet expectedPet = new Pet();

        mockPetStore.expectAndReturn("getPet", C.args(new IsEqual(new Long(1))), existingPet);

        String result = action.execute();

        assertEquals(Action.SUCCESS, result);
        assertEquals(expectedPet, existingPet);

    public void testViewPetNoId() throws Exception {
        mockPetStore.expectAndReturn("getPet", C.ANY_ARGS, null);

        String result = action.execute();

        assertEquals(Action.ERROR, result);
        assertEquals(1, action.getActionErrors().size());
        assertEquals("Invalid pet selected.", action.getActionErrors().iterator().next());

    public void testViewPetInvalidId() throws Exception {

Testing Interceptors and/or Result Types

Check out the test suites in XWork and Struts 2. These suites are comprehensive and provide a good starting point. Here's an example.

Test for ParametersInterceptor
public void testDoesNotAllowMethodInvocations() {
    Map params = new HashMap();
    params.put("@java.lang.System@exit(1).dummy", "dumb value");

    HashMap extraContext = new HashMap();
    extraContext.put(ActionContext.PARAMETERS, params);

    try {
        ActionProxy proxy = ActionProxyFactory.getFactory().
               createActionProxy("", MockConfigurationProvider.MODEL_DRIVEN_PARAM_TEST, extraContext);
        assertEquals(Action.SUCCESS, proxy.execute());

        ModelDrivenAction action = (ModelDrivenAction) proxy.getAction();
        TestBean model = (TestBean) action.getModel();

        String property = System.getProperty("action.security.test");
    } catch (Exception e) {

(lightbulb) The framework also provides built-in support for JUnit 3.8 via an abstract StrutsTestCase, which provides common Struts variables and setup code.