Seasar DI Container with AOP

S2StrutsUnit Outline

S2StrutsUnit has the following characteristics.

  • UnitTest functionality using ServletAPI
  • As with S2Unit, testing is possible through the use of mock ServletAPI objects. The difference between S2Unit is the ability to define a return value from ServletAPI in the component definition file.

  • Testing functionality of the Action subclass of Struts
  • Testing the Action class is very easy. Various tests are done simply by passing the required parameters as method arguments. These include the Request value reference testing, ActionForm referencing testing, MappingDispatchAction testing, and testing in a requested path.

S2StrutsUnit Reference

Creating Test classes using S2StrutsUnit

Test classes are created by inheriting org.seasar.struts.unit.S2StrutsTestCase . S2StrutsTestCase is an expansion of the S2TestCase class, and uses the same test methods as S2TestCase and JUnit.

S2StrutsUnit Functionality

S2StrutsUnit contains the following functionality to make S2Struts development testing easier.

Execute Action with a selected Action
Pass the instance of Action as an argument to S2StrutsTestCase#execute(Action) to run an Action class with injected services.
The S2StrutsTestCase#execute(Class) method provides identical functionality without creating an instance of the Action class.
The resulting return value will be the forward name of the ActionMapping returned by Action.
Execute Action with a selected Action and ActionForm
Pass the instance of Action and ActionForm as arguments to S2StrutsTestCase#execute(Action,ActionForm) to run an Action class with services injected.
The S2StrutsTestCase#execute(Class, ActionForm) method provides identical functionality without creating an instance of the Action class.
The resulting return value will be the forward name of the ActionMapping returned by Action.
Execute MappingDispatchAction
Pass the instance of Action, ActionForm, and method name as arguments to S2StrutsTestCase#execute(Action, ActionForm,String) to run an Action class with services injected. The S2StrutsTestCase#execute(Action,String) method can be used without an ActionForm.
The S2StrutsTestCase#execute(Class,ActionForm,String) method provides identical functionality without creating an instance of the Action class. The S2StrutsTestCase#execute(Class,String) method can also be used.
The resulting return value will be the forward name of the ActionMapping returned by Action.
Execute Action using a selected path
Pass the selected path name as an argument to the S2StrutsTestCase#execute(String) method to create and run a corresponding Action class with services injected.
The resulting return value will be the forward name of the ActionMapping returned by Action.
When running an Action class by selecting a path, context read-in as described below is required.
Context Read-in
The setDocBase(String) method is used to read-in values from struts-config.xml in the selected path. The WEB-INF directory must exist directly within the selected directory.
The arguments path is searched from the selected resources. As with S2TestCase#include(String), either the full path or relative path from the test class can both be used to select a path.

Example

Basic testing of the Action class

  • Inherit S2StrutsTestCase.
  • Include s2struts.dicon and the component definition file with the HttpServletAPI mock definitions using setUp().
  • Include the definition of the Action and Service components from the definition file using setUpExecuteTrue().
  • Run TestAction using execute(TestAction.class) of testExecuteTrue().
  • Test the Forward target using the execute method return value, ActionMessages using getActionMessages(), and ActionErrors using getActionErrors().
public class TestActionTest extends S2StrutsTestCase {

    public TestActionTest(String name) {
        super(name);
    }

    protected void setUp() {
        include("HttpMockObject.dicon");
        include("s2struts.dicon");
    }

    public void setUpExecuteTrue() {
        include("TestServiceReturnTrue.dicon");
    }

    public void testExecuteTrue() throws Exception {
        String actual = execute(TestAction.class);

        assertEquals("success", actual);

        String[] values = {"val1", "val2", "val3", "val4", "val5"};
        ActionMessages expectedMessages = new ActionMessages();
        expectedMessages.add("message", new ActionMessage("messageKey", values));
        assertEquals(expectedMessages, getActionMessages());
    }
}
	

Testing the Action class using ActionForm

  • Create an instance of ActionForm, and run the Action after configuring the instance with the test values.
public class FormTestActionTest extends S2StrutsTestCase {

    ...

    public void testGetValueFromForm() throws Exception {
        TestActionForm form = new TestActionForm();
        form.setVal("foo");

        String actual = execute(FormTestAction.class, form);

        assertEquals("foo", actual);
    }
}
	

Testing the MappingDispatchAction class

  • Pass the variable "foo" as the argument of the execute method to test the method foo(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse).
public class MappingDispatchActionTest extends S2StrutsTestCase {

    ...

    public void testFooMethod() throws Exception {
        String actual = execute(DispatchTestAction.class, "foo");
        assertEquals("success", actual);
    }
}
	

Testing using Context Read-in

  • Select a directory containing context files using setDocBase(String) in setUp().
  • Instances of Action actually run can be acquired using getExecutedAction().
  • Instances of ActionForm actually run can be acquired using getExecutedActionForm().
public class LoadWebContextTest extends S2StrutsTestCase {

    ....

    protected void setUp() {
        ...
        setDocBase("org/seasar/struts/unit");
    }

    public void testExecuteString() throws Exception {
        String actual = execute("/login?id=id&pass=pass");

        assertEquals("success", actual);
        assertEquals(LoginAction.class, getExecutedAction().getClass());
        assertNull(getExecutedActionForm());
    }
}