Unit Testing Struts 2 Actions wired with Spring using JUnit

Hopefully this entry serves as some search engine friendly documentation on how one might unit test Struts 2 actions configured using Spring, something I would think many, many people want to do. This used to be done using StrutsTestCase in the Struts 1.x days but Webwork/Struts provides enough flexibility in its architecture to accommodate unit testing fairly easily. I’m not going to go over how the Spring configuration is setup. I’m assuming you have a struts.xml file which has actions configured like this:

<struts>
 <package namespace="/site" extends="struts-default">
  <action name="deletePerson" class="personAction"
                  method="deletePerson">
   <result name="success">/WEB-INF/pages/person.jsp</result>
  </action>
 </package>
 ...
</struts>

You also might have an applicationContext.xml file where you might define your Spring beans like this.

<beans>
 <bean id="personAction"
  class="com.arsenalist.action.PersonAction"/>
 ...
</beans>

Then of course you also need to have an action which you want to test which might look something like:

public class PersonAction extend ActionSupport {

  private int id;

  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String deletePerson() {
    ....
    return SUCCESS;
  }
}

Remember than in Struts 2, an action is usually called before and after various other interceptors are invoked. Interceptor configuration is usually specified in the struts.xml file. At this point we need to cover three different methods of how you might want to call your actions.

  1. Specify request parameters which are translated and mapped to the actions domain objects (id in the PersonAction class) and then execute the action while also executing all configured interceptors.
  2. Instead of specifying request parameters, directly specify the values of the domain objects and then execute the action while also executing all configured interceptors.
  3. Finally, you just might want to execute the action and not worry about executing the interceptors. Here you’ll specify the values of the actions domain objects and then execute the action.

Depending on what you’re testing and what scenario you want to reproduce, you should pick the one that suits the case. There’s an example of all three cases below. The best way I find to test all your action classes is to have one base class which sets up the Struts 2 environment and then your action test classes can extend it. Here’s a class that could be used as one of those base classes.

See the comments for a little more detail about whats going on. One point to note is that the class being extended here is junit.framework.TestCase and not org.apache.struts2.StrutsTestCase as one might expect. The reason for this is that StrutsTestCase is not really a well written class and does not provide enough flexibility in how we want the very core Dispatcher object to be created. Also, the interceptor example shown in the Struts documentation does not compile as there seems to have been some sort of API change. It’s been fixed in this example.

import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;
import junit.framework.TestCase;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.views.JspSupportServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ContextLoader;

import java.util.HashMap;

/**
 * @author Zarar Siddiqi
 */
public abstract class BaseStrutsTestCase extends TestCase {
    private static final String CONFIG_LOCATIONS = "META-INF/applicationContext-app.xml," +
                "META-INF/applicationContext-security.xml";
    private static ApplicationContext applicationContext;
    private Dispatcher dispatcher;
    protected ActionProxy proxy;
    protected static MockServletContext servletContext;
    protected static MockServletConfig servletConfig;
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;

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

    /**
     * Created action class based on namespace and name
     * @param clazz Class for which to create Action
     * @param namespace Namespace of action
     * @param name Action name
     * @return Action class
     * @throws Exception Catch-all exception
     */
    @SuppressWarnings("unchecked")
    protected <t> T createAction(Class<t> clazz, String namespace, String name)
            throws Exception {

        // create a proxy class which is just a wrapper around the action call.
        // The proxy is created by checking the namespace and name against the
        // struts.xml configuration
        proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class).
                createActionProxy(
                namespace, name, null, true, false);

        // by default, don't pass in any request parameters
        proxy.getInvocation().getInvocationContext().
                setParameters(new HashMap());

        // do not execute the result after executing the action
        proxy.setExecuteResult(true);

        // set the actions context to the one which the proxy is using
        ServletActionContext.setContext(
                proxy.getInvocation().getInvocationContext());
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        ServletActionContext.setRequest(request);
        ServletActionContext.setResponse(response);
        ServletActionContext.setServletContext(servletContext);
        return (T) proxy.getAction();
    }

    protected void setUp() throws Exception {
        if( applicationContext == null ) {
            // this is the first time so initialize Spring context
            servletContext = new MockServletContext();
            servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
                    CONFIG_LOCATIONS);
            applicationContext = (new ContextLoader()).initWebApplicationContext(servletContext);

            // Struts JSP support servlet (for Freemarker)
            new JspSupportServlet().init(new MockServletConfig(servletContext));
        }
        // Dispatcher is the guy that actually handles all requests.  Pass in
        // an empty. Map as the parameters but if you want to change stuff like
        // what config files to read, you need to specify them here.  Here's how to
        // scan packages for actions (thanks to Hardy Ferentschik - Comment 66)
        // (see Dispatcher's source code)
        HashMap params = new HashMap();
        params.put("actionPackages", "com.arsenalist.action");
        dispatcher = new Dispatcher(servletContext, params);
        dispatcher.init();
        Dispatcher.setInstance(dispatcher);
    }
}

By extending the above class for our action test classes we can easily simulate any of the three scenarios listed above. I’ve added three methods to PersonActionTest which illustrate how to test the above three cases: testInterceptorsBySettingRequestParameters, testInterceptorsBySettingDomainObjects() and testActionAndSkipInterceptors(), respectively.

public class PersonActionTest extends BaseStrutsTestCase {

 /**
  * Invoke all interceptors and specify value of the action
  * class' domain objects directly.
  * @throws Exception Exception
  */
 public void testInterceptorsBySettingDomainObjects()
         throws Exception {
  PersonAction action = createAction(PersonAction.class,
                "/site", "deletePerson");
  action.setId(123);
  String result = proxy.execute();
  assertEquals(result, "success");
 }

 /**
  * Invoke all interceptors and specify value of action class'
  * domain objects through request parameters.
  * @throws Exception Exception
  */
 public void testInterceptorsBySettingRequestParameters()
                     throws Exception {
  createAction(PersonAction.class, "/site", "deletePerson");
  request.addParameter("id", "123");
  String result = proxy.execute();
  assertEquals(result, "success");
 }

 /**
  * Skip interceptors and specify value of action class'
  * domain objects by setting them directly.
  * @throws Exception Exception
  */
 public void testActionAndSkipInterceptors() throws Exception {
  PersonAction action = createAction(PersonAction.class,
                  "/site", "deletePerson");
  action.setId(123);
  String result = action.deletePerson();
  assertEquals(result, "success");
 }
}

The source code for Dispatcher is probably a good thing to look at if you want to configure your actions more specifically. There are options to specify zero-configuration, alternate XML files and others. Ideally the StrutsTestCaseHelper should be doing a lot more than what it does right now (creating a badly configured Dispatcher) and should allow creation of custom dispatchers and object factories. That’s the reason why I’m not using StrutsTestCase since all that does is make a couple calls using StrutsTestCaseHelper.

If you want to test your validation, its pretty easy. Here’s a snippet of code that might do that:

 public void testValidation() throws Exception {
  SomeAction action = createAction(SomeAction.class,
                  "/site", "someAction");
  // lets forget to set a required field: action.setId(123);
  String result = proxy.execute();
  assertEquals(result, "input");
  assertTrue("Must have one field error",
                  action.getFieldErrors().size() == 1);
 }
 

This example uses Struts 2.0.11 and Spring 2.0.6. That about sums things up.

Updated on November 4th, 2007 to make it a little simpler and compatible for newer versions of Struts and Spring. Thanks to Haroon Rafique.

About these ads

157 thoughts on “Unit Testing Struts 2 Actions wired with Spring using JUnit

  1. Rick Evans

    That is nice, very useful.

    Any chance of it being submitted as a patch to the S2 team so that it can be ‘polished’ (Javadoc’ced and suchlike) and included in the core, similar to the way that Spring provides integration test support classes in the spring-mock.jar library?

    Cheers
    Rick

    Reply
  2. ki_ll

    Thank you, it works great. You can also set the session into invocationContext:

    protected static HashMap session = new HashMap();
    // …

    protected Object createAction(Class clazz, String namespace, String name)
    throws Exception {
    // …
    proxy.getInvocation().getInvocationContext().setParameters(new HashMap());
    proxy.getInvocation().getInvocationContext().setSession(session);
    //…
    }

    Reply
  3. Mike

    This code has helped me a lot to get my action tests up and running. Thank you very much. A little problem remains, though, as we are using tiles too. When ever I try to execute an action that has as result type “tiles” I get an NPE in org.apache.struts2.view.tiles.TileResult.doExecute. It looks like the tiles listener (configured in web.xml) is not loaded. How does your code load web.xml actually (if at all)?

    Reply
  4. arsenalist

    Mike, the web.xml is not being loaded at all. I didn’t find the need for it in my test cases but that would be an excellent feature. If I have some time I’ll see where/how it can be loaded, if you figure it out, please post the solution…

    Reply
  5. Thomas

    This code is very helpful! It is exactly what I was looking for. However, I also have the same problem with tiles (as Mike above). If someone discovers the solution please post. I was going down the route of putting the tiles.xml in the ConfigurationManager, but I think this is the wrong path.

    Reply
  6. Max

    This is great thankyou!
    My web application uses a servlet filter to maintain my jpa entity manager open in my views (org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter). I’m new to unit testing and wondered how i could modify your base class so that my filter gets inluded before calling the struts dispatcher. I am guessing that this is a similar problem to the tiles problem that is described in a previous post, ie the need to load web.xml.

    Reply
  7. Devarajan

    Very good tutorial and it was very useful for me to test the complete flow in my application Thanks a lot.

    Reply
  8. Scott

    I cannot find an invoke() method on the proxy reference in your testValidation()example. Also, I had to modify the method signature of protected T createAction(Class clazz, String namespace, String name) as follows protected T createAction(Class clazz, String namespace, String name)

    Can you explain please?

    Thanks and great work!

    Reply
  9. Scott

    I see what happened to your source code now! the got sripped down on account of the HTML symbols! I have wrapped the method signature inside tags and it looks fine

    protected createAction(Class clazz, String namespace, String name)

    Now then, can you tell me why I am unable to find the invoke method on my proxy reference in the validation test code?

    Sweet examples brother,
    Scott

    Reply
    1. Mohamed Farouk

      Hello Scott
      I am getting a compile error on the method signature can you post the correct method signature plz?

      — Error (T) cannot be resolved :
      protected T createAction(Class clazz, String namespace, String name)

      — Error (T) cannot be resolved :
      return (T) proxy.getAction();

      Reply
  10. Scott

    Can you tell me how to get around the error caused by this jndi snafu? This is being thrown from the setUp() at appContext.refresh();

    Reply
  11. arsenalist

    What do you mean? The return value of createAction() is the action class. Whatever public methods are available in your action are available during the tests. Just have a method which returns a model object in your action..

    Reply
  12. Scott

    Consider the following test:
    PayrollUpdateAction action = createAction(PayrollUpdateAction.class,
    “/pr”, “PayrollUpdate_notes”);
    action.setId(123L);
    String result = proxy.execute();
    PayrollUpdateTO model = action.getModel();
    System.out.println(model);
    assertEquals(“notes”,result );

    The model that prints is null! Spring injected the model and it was a live reference during the execute() behavior! I guess my question is one of accessing the action before it has wound it’s way through the interceptors during the return cycle

    Reply
  13. Scott

    createAction(PayrollUpdateAction.class,“/pr”, “PayrollUpdate_notes”);

    Are you only requiring the class name because Java Generics require it?

    Reply
  14. Scott

    I agree this makes your factory method much cleaner! I really appreciate you offering up this solution. It is easy to see you are a very sharp cat.

    Reply
  15. Scott

    I have an Action that extends a parent who implements SessionAware. When I pass my subclass to the createAction and use the proxy to invoke it, I am getting a null pointer on the session Map. Should the setSession()be getting called if it is in the interceptor stack?

    Scott

    Reply
  16. arsenalist

    I haven’t worked with SessionAware so I’m not sure when it gets invoked, I just use the org.apache.struts2.ServletActionContext class to get hold of the request/response objects.

    Reply
  17. Scott

    All the interceptors are being called, however, since we are using a MockServletContext, naturally the request and session are Mocks too. I am trying to test an Action that implements these *aware* interfaces and of course the request and session maps are null. What would be the most natural way to push my own maps into these variables? Does it seem okay to simply call the setters on the action itself?

    Thanks,
    Scott

    Reply
  18. Scott

    One more comment on the interceptor invocation. I have the following test running:

    MemberMaintenance action = createAction(MemberMaintenance.class,”/member”, “MemberMaintenance_add”);
    (MemberMaintenance) proxy.getAction()).setSession(new HashMap());
    Map p = new HashMap();
    p.put(“id”, “1”);
    proxy.getInvocation().getInvocationContext().setParameters(p);
    String nextStep = proxy.execute();
    (ActionSupport) proxy.getAction()).getFieldErrors();

    and when the SessionAware interceptor is invoked, it is “resetting” my session Map to null! I have fixed it for the moment by changing the setSession(Map session) to check for a null Map being passed in. This really calls into question the advantage of having the interceptors included on the proxy.execute() test though. Comments?

    Scott

    Reply
  19. Jason

    Hello,

    I’m completely new to unit testing with struts and I’m having difficulty getting this example to work… Is this designed to run in the container or not? I thought not, but now I’m unsure. If it’s in container, can anyone point me to some examples of setting up container testing with junit?

    I’m also seeing an error saying either my applicationContext.xml cannot be found, or there’s a connection timeout parsing the xml. The file not found is fair enough, but can anyone suggest a reason for a connection timeout?

    Reply
  20. arsenalist

    Jason, this is not designed to run in a container. I don’t think running unit tests in a container is the way to go, you’re testing discrete action functionality, not how the container might behave.

    As for the appplicationContext.xml not being found, remember that they’re being loaded from the classpath using XmlWebApplicationContext so make sure your application context configuration is in your classpath and referenced correctly in config[]. If you prefer to load them from the file system instead, you can just override XmlWebApplicationContext.getResource().

    Reply
  21. Jason

    Thanks for your help. I seem to be locating the appContext ok now, but I have another question: No where in the code do you specify the location of the struts.xml. Is this also assumed to be on the classpath? Is there a way to load this from the filesystem if necessary?
    I’m getting an error now where none of my action mappings can be resolved, so I assume struts.xml to not being picked up.

    Reply
  22. samuel

    Get this error:

    Unable to create SAX parser – Class: org.gjt.xpp.jaxp11.SAXParserFactoryImpl
    etc etc etc
    Caused by: javax.xml.parsers.ParserConfigurationException: validation is not supported
    at org.gjt.xpp.jaxp11.SAXParserFactoryImpl.newSAXParser(SAXParserFactoryImpl.java:100)
    at com.opensymphony.xwork2.util.DomHelper.parse(DomHelper.java:109)
    … 21 more

    The app code works on the server. Is some jar missing to my local test?

    Reply
  23. Alan

    The line:

    StrutsSpringObjectFactory.setObjectFactory(ssf);

    Does not works in struts 2.1 snapshot.

    I trace the code and find out that ObjectFactory in xworks 2.1 drop the singleton pattern. Would you mind giving me any suggestion to do the right same things in struts 2.1 (+ xworks 2.1)

    Reply
  24. Alan

    Your sample is different from my case. Since I have other types of test that use spring, and I don’t want to startup more than one spring application context. In short, I wish to have a way that put a spring application context into struts context by hand when test setup, just as your previous sample that I can put the application context in a new StrutsSpringObjectFactory and assign the object factory to struts context. Is there any other way to do that? Thanks for your help.

    Reply
  25. Amin

    Hi there

    I have recently started developing Struts 2 and I am enjoying it, thanks to articles like these. I have used the base class and I am getting a NPE when it comes to tiles..and i see people have posted about this before and I was wondering if someone has done a solution and if so is there any sample code I could have a look at?

    Thanks

    Reply
  26. Felix

    Hi, thanks for this nice article. It helped me a lot.

    Maybe you can fix the typo(?) in PersonActionTest line 12 (pa->action).

    I am using Struts 2.0.11 with Tiles 2.0.4 (TilesDecorationFilter) and I am not facing the problems stated above. But I am extremely new to all the stuff ^^ so don’t trust me too much :)

    Reply
  27. Mike

    I am also struggling to get this to work without Springs (just struts2). I know that isn’t the purpose of this article, but its so close :)

    Reply
  28. Jon

    Hi there

    I am trying this out for a project that uses freemarker and I am getting a ton of warning messages – I am testing it in intellij idea


    java.io.FileNotFoundException: class path resource [template/simple/common-attributes_en_US.ftl] cannot be resolved to URL because it does not exist

    2007-11-28 16:45:10,546 WARN [org.springframework.mock.web.MockServletContext:MockServletContext.java:264] : Couldn’t determine real path of resource class path resource [template/xhtml/form-close-validate.ftl]
    java.io.FileNotFoundException: class path resource [template/xhtml/form-close-validate.ftl] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Documents%20and%20Settings/jon/.m2/repository/org/apache/struts/struts2-core/2.0.11/struts2-core-2.0.11.jar!/template/xhtml/form-close-validate.ftl

    any tips?

    Reply
  29. Rishi Kant Sharma

    Hi

    I am learning struts2. this is realy good artical for me.
    if you have more artical related to struts2,Spring and hibernate published it.

    Rishi

    Reply
  30. Mohammed

    Hi,

    Really good example. Had a quick (and probably stupid) question though. How do you invoke a method in the action other than “execute”? I have an action class with all my CRUD methods in there and want to test each one with the full interceptor stack etc.

    Thanks,

    Mohammed

    Reply
  31. arsenalist

    Mohammed, you can define the configuration in your struts.xml:

    <action name="myAction" class="MyClass" method="myMethod"/>

    or you can just invoke myAction like this: myAction!myMethod.action and myMethod will be called.

    Reply
  32. Mohammed

    Thanks,

    Do you, or anyone else have this working with Maven 2?

    I’m having problems…

    (NB, my other unit tests work fine – just cant test action classes at the moment).

    I wasnt sure how to get the XML files (Spring, Struts etc) onto the test classpath, so ended up copying them to src/test/resources (there has to be a better way!)

    After doing that, i’m getting the following error

    Unable to load bean: type:org.apache.struts2.components.template.TemplateEngine class:org.apache.struts2.components.template.JspTemplateEngine – bean – jar:file:/C:/Documents%20and%20Settings/abdealim/.m2/repository/org/apache/struts2/struts2-core/2.0.9/struts2-core-2.0.9.jar!/struts-default.xml:34:150
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:208)

    Reply
    1. Anand

      Add the struts2-core-2.0.9.jar in your pom file and test once ,

      Also you can add the same in properties-> Javabuild path-> add external jars-> add this jar and test.

      Reply
  33. Sri

    Hey,

    I tried running the test customized to my application, but I am encountering an issue of Infinite Recursion Detected. I guess its something related to auto chaining not working, but am not sure. Appreciate your help in this regard

    I am getting the same error when running the junit test.
    Infinite recursion detected: [/login!input, /exceptionHandler, /exceptionHandler] – [unknown location]
    at com.opensymphony.xwork2.ActionChainResult.execute(ActionChainResult.java:207)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    at com.opensymphony.xwork2.ActionChainResult.execute(ActionChainResult.java:229)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    at gov.glin.web.struts.action.LoginActionTest.testValidLogin(LoginActionTest.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    here is the entry in the struts.xml

    { “input”, “login” }
    /login/login.ftl
    /home.action
    /changePassword.action?user=${user.userId}

    Reply
  34. Arsenalist

    Re: 45.

    src/test/resources and src/main/resources is the best way to put stuff in your classpath, not sure why you don’t like that. You can also define additional resources location in Maven if you need to.

    Reply
  35. dalto

    Hi,

    I’m very new to java, struts, spring, etc.. :P
    And i’m getting trouble to get this test working with my struts actions. I’m using eclipse and when I run the test I see from the Junit tab this error:

    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:99)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:317)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:125)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:141)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:123)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:91)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:94)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:292)
    at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:156)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184)
    at com.aix2.test.BaseStrutsTestCase.setUp(BaseStrutsTestCase.java:117)
    at junit.framework.TestCase.runBare(TestCase.java:125)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

    I tryed to test if I was pointing to the right file and if it is readable putting this code snipet in my class constructor:


    File f = new File("WEB-INF/applicationContext.xml");
    if (f.exists() && f.isFile()) {
    try {
    System.out.println("file exists: " + f.getCanonicalPath());
    } catch (Exception e) {
    e.printStackTrace();
    }
    if (f.canRead()) {
    System.out.println("readable archive.");
    }
    }

    which prints:
    “file exists.
    readable archive”

    and then:

    web-15:23:54,968 DEBUG com.aix.estagio.AtividadesEstagioAction:41 - AtividadesEstagioActionTest() - start
    web-15:23:54,984 DEBUG com.aix2.test.BaseStrutsTestCase:88 - createAction(Class, String, String) - Erro ao criar ActionProxy: java.lang.NullPointerException
    web-15:23:54,984 ERROR com.aix2.test.BaseStrutsTestCase:90 - createAction(Class, String, String)
    java.lang.NullPointerException
    at com.aix2.test.BaseStrutsTestCase.createAction(BaseStrutsTestCase.java:76)
    at com.aix.test.webgiz.AtividadesEstagioActionTest.(AtividadesEstagioActionTest.java:45)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
    at junit.framework.TestSuite.createTest(TestSuite.java:131)
    at junit.framework.TestSuite.addTestMethod(TestSuite.java:114)
    at junit.framework.TestSuite.(TestSuite.java:75)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.getTest(JUnit3TestLoader.java:102)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.loadTests(JUnit3TestLoader.java:59)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

    after debugging I’ve found that the dispatcher object is null when in the createAction method.

    When is this object instantiated?
    Do you have any ideas on how to get this working for my case?

    Many thanks and sorry for the bad english.

    Reply
    1. Aaron

      use like this:
      CONFIG_LOCATIONS = “file:D:\\开发中项目\\dsunit\\WebRoot\\WEB-INF\\classes\\applicationContext.xml”;

      Hope you success.

      Reply
  36. Arsenalist

    dalto, you’re having a classpath issue. Just make sure your tests can find the application context at /WEB-INF/applicationContext.xml. Or you can tell Spring where to look for the files as shown in the example.

    Reply
  37. Arsenalist

    Sri, that to me looks like something wrong with your action configuration which is causing a recursion between the login!input action and your exceptionHandler. Is your exception handler by any chance redirecting to login!input and in turn login!input is throwing n exception thus causing the recursion?

    I believe the action is being invoked correctly which indicates that the test is setup fine, it’s just what happens after that is the problem.

    Reply
  38. Sri

    here is my struts entry

    { “input”, “login” }
    /login/login.ftl
    /home.action
    /changePassword.action?user=${user.userId}

    and my applicationContext entry

    I dont know from where the input method is invoked, and why my action fails which in turn calls exceptionHandler. frustrated with this.

    Reply
  39. Sri

    applicationContext

    struts entry

    { “input”, “login” }
    /login/login.ftl
    /home.action
    /changePassword.action?user=${user.userId}

    Reply
  40. Arsenalist

    This blog has some trouble dealing with XML, you’ll have to escape it when posting code. I think the “input” method is automatically invoked by Struts at some point (I could be wrong), you should try renaming the input method to something more precise and see what the behavior is.

    Reply
  41. dalto

    thanks for the reply!
    i’m new to java and i can’t understand what the classpath have to do with this issue.. do i have to add my project directory to the classpath?
    i already tried to fix this by specifying to where the file is like the exemple and it keep throwing the same error.. by the way, i only have the applicationContext.xml file and not the security one, can this cause any problem?

    Reply
  42. dalto

    i’ve added the MockServletContext package directory to the classpath, keeps throwing the same error in the setUp() method.

    Reply
  43. dalto

    i solved it by referencing the file this way:
    file///c:/workpath/project/WEB-INF/applicationContext.xml

    nice article, btw ;)

    Reply
  44. Sri

    hi arsenalist,

    Issue 46 I could resolve that issue now, the problem was there was an NPE which was thrown in ContextInterceptor because I didnt set the Session in the BaseTestCase

    proxy.getInvocation().getInvocationContext().setSession(sessionMap);

    and hence the infinite recursion. Good Lord I spent lot of time debugging for this. My bad.

    Thanks for your help arsenal.

    Reply
  45. David Clarke

    Thanks for posting this, in an ocean of mediocrity it is nice to stumble across the odd pearl.

    I’m prototyping my first struts2 app right now and having successfully configured actions/results using annotations I would like to be able to test them. Unfortunately as it stands and using your base test class I am unable to do that. Everything works as expected if I add the actions/results into struts.xml. I have already spent some time wading through the Dispatcher source and find myself very little the wiser. I thought perhaps by loading actionPackages in the initParams parameter of the Dispatcher constructor it might all magically start working. Unfortunately I’m still relatively new to java (from c#) and I really want to understand what I’m doing rather than rely on magic. I would really appreciate a few pointers.

    Reply
  46. David Clarke

    Well, I would like to know how to enable your BaseStrutsTestCase class to work with action/result annotations.

    Reply
  47. Mark

    I also develop an application using Tiles. It would be really neccessary to mock out the TilesResult because these would be tested independently.

    I would also like to test Preparer classes, but my preparers could do nothing but return action results so that they can be tested.

    Reply
  48. Mohammed

    Hi,

    I’m getting this error, and have no idea where to even start looking for the problem. I’m using Maven 2, JUnit 4, Struts 2 + Spring.

    Unable to load bean: type:org.apache.struts2.components.template.TemplateEngine class:org.apache.struts2.components.template.JspTemplateEngine – bean – jar:file:/C:/Documents%20and%20Settings/abdealim/.m2/repository/org/apache/struts2/struts2-core/2.0.9/struts2-core-2.0.9.jar!/struts-default.xml:34:150
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:208)
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:101)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:131)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
    at com.sentaca.telusreporting.web.BaseStrutsTestCase.setUp(BaseStrutsTestCase.java:95)
    at com.sentaca.telusreporting.web.UserManagementActionTestCase.setUp(UserManagementActionTestCase.java:12)
    at junit.framework.TestCase.runBare(TestCase.java:132)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:138)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:125)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:132)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:290)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:818)
    Caused by: java.lang.NoClassDefFoundError: javax/servlet/jsp/JspWriter
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2357)
    at java.lang.Class.getDeclaredConstructors(Class.java:1808)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:198)
    … 25 more

    Reply
  49. David Clarke

    Hey there – I guess your attention is currently elsewhere. I am wondering if you have had any opportunity to think about how your BaseStrutsTestCase could be made annotation-aware. I realise this may not be high on your list of priorities but I would certainly appreciate some pointers as to how I might go about this. I’m currently in the middle of a prototype exercise and I will need to revisit this soonish. Being able to test my actions is higher on my list of priorities than using annotations so I may need to revert to the xml-style configuration if I can’t get this working.

    Reply
  50. Hardy Ferentschik

    It is actually quite easy to make this base class annotation aware. Just add this before line 86 (in setUp()) in BaseStrutsTestCase:


    HashMap params = new HashMap();
    params.put("actionPackages", "your.action.package1,your.action.package1");

    and then modify line 86 to read:


    dispatcher = new Dispatcher(servletContext, params);

    You basically need to tell the dispatcher to scan your classpath. actionPackages is the parameter which makes this happen in web.xml. If you want to understand why and how this works check out the Dispatcher code at Krugle:

    http://www.krugle.org/kse/files/svn/svn.apache.org/struts/current/struts2/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java

    Reply
  51. Ismath

    I’m getting this error when I run my JUnit Test class

    java.lang.SecurityException: class “junit.framework.JUnit4TestCaseFacade”‘s signer information does not match signer information of other classes in the same package
    at java.lang.ClassLoader.checkCerts(Unknown Source)
    at java.lang.ClassLoader.preDefineClass(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at org.junit.internal.runners.OldTestClassRunner$OldTestClassAdaptingListener.asDescription(OldTestClassRunner.java:41)
    at org.junit.internal.runners.OldTestClassRunner$OldTestClassAdaptingListener.startTest(OldTestClassRunner.java:31)
    at junit.framework.TestResult.startTest(TestResult.java:151)
    at junit.framework.TestResult.run(TestResult.java:103)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

    Any idea on what this means ? Thanks !

    Ismath

    Reply
  52. felix

    Hi, just one idea to improve the code. In class PersonActionTest you use

    assertEquals(result, “success”);

    which will result in some misleading error messages by JUnit on failure, like

    [junit] junit.framework.ComparisonFailure: expected: but was:

    The JUnit API documentation states:

    assertEquals(java.lang.String expected, java.lang.String actual)

    So just flipping the variables will make things clearer.

    Greets, Felix

    Reply
  53. Andrew

    This really works well, but I have an obstacle. We have custom validators and have placed the validators.xml file in the /WEB-INF/classes/ directory as prescribed. The site works fine, however, the test case for the action does not. Running against the action produces the exception: java.lang.IllegalArgumentException: There is no validator class mapped to the name myCustomValidator.

    I’m not sure how to incorporate this into the above test configuration. Do you have a suggestion? Thanks!

    Reply
  54. Andrew

    Actually, I just figured it out, but perhaps you know a better way. I added:

    ValidatorFactory.registerValidator(“myCustomValidator”, “x.y.z.web.validators.MyCustomValidator”);

    as the first line in my unit test and it works. By the way, I’m also using your base test to get an action so that I can set up the validator context in other tests that test validators independently. It really made it easy. Thanks again.

    Reply
  55. Timothy

    Hi, thanks for this post. My colleagues and I are finding it very helpful.

    Just one nit-picking I couldn’t resist though.
    It seems you are using assert…() method’s parameters in reverse order. According to the API doc, ‘expected’ value should come first, then ‘actual’. I don’t think this would degrade the quality of tests in terms of accuracy, but for the people reading the tests (and adopting these in their own projects), it may be confusing at times since it goes against the usual convention.

    Anyways, cheers for the Arsenal!

    Reply
  56. Jignesh

    Hi,I am having struts2 + hibernate application no spring configuration.

    I used the same abstract BaseStrutsTestCase class with only change to commented out this code from setUp() method
    /*
    if( applicationContext == null ) {
    // this is the first time so initialize Spring context
    servletContext = new MockServletContext();
    servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
    CONFIG_LOCATIONS);
    applicationContext = (new ContextLoader()).initWebApplica
    */

    Test cases works fine with struts2 application only.

    But the only problem is that i am not able to pass the request parameters to the action class while able to pass the
    params by SettingDomainObjects.

    It means testInterceptorsBySettingDomainObjects() this is working fine while this is not testInterceptorsBySettingRequestParameters() from the above example.

    Can anybody tell me what is the problem here ?

    Reply
  57. jaalex

    If we want to use a different struts xml than the default one (say, struts-test.xml) , it can be specified as :

    params.put(“config”,”struts-default.xml,struts-plugin.xml,struts-test.xml”);

    Reply
  58. Bigster

    Hi all,

    I´ve read the posts, and someone has the need to put in the CONFIG_LOCATIONS the web-xml?

    I´ve there de JNDI datasources needed by Spring.

    How, based in the example, could i load to the classpath the web.xml??

    cheers

    Reply
  59. arsenalist

    Bigster, to load config locations, you can just specify it using the way it’s shown in Line 75 of the setUp() method.

    servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, CONFIG_LOCATIONS);

    Reply
  60. Bigster

    arsenalist,

    That´s what i´ve tried :

    private static final String CONFIG_LOCATIONS =
    “WEB-INF/web.xml,”+ “hibernate.cfg.xml,” + “aplicationContext-dao.xml,” + “applicationContext-resources.xml,” + “applicationContext-service.xml,” + “applicationContext-struts.xml”;

    it throws a FileNotfoundExecption, it don´t find tha web.xml in the WEB-INF folder.

    any help?

    Reply
    1. Arindam Ray

      use like this:

      private static final String CONFIG_LOCATIONS =
      “/WEB-INF/web.xml,”+ “/hibernate.cfg.xml,” + “/aplicationContext-dao.xml,” + “/applicationContext-resources.xml,” + “/applicationContext-service.xml,” + “/applicationContext-struts.xml”;

      Reply
  61. Tavo

    Hi. I’m trying to implement this solution, but i have problem with the import
    “import org.springframework.mock.web.MockHttpServletRequest;”, don’t recognises and don’t exist in spring 2.0.4 jar file. Any idea?. Congratulations Arsenal and thank you for your article.

    Reply
  62. Tavo

    Resolved. The spring jar file does’n contain the mock jar, is only the core. Must be include the mock jar file in the classpath. Now i’m trying the test my classes. Thks.

    Reply
  63. vd greg

    Hi I am very new to struts2. I am using tiles and like to use your code to develop some test cases for my application. The problem is I was using any namespace tag before in the package area.
    So when I use namespace tag and put some value in it, the body jsp displayed but not the left menu, top menu and etc.

    Why other jsp are not getting displayed?
    —Struts.xml———————————————–

    myTest
    Error

    ————————————————————
    Tiles.xml

    Reply
  64. vd greg

    Hi I am very new to struts2. I am using tiles and like to use your code to develop some test cases for my application. The problem is I was using any namespace tag before in the package area.
    So when I use namespace tag and put some value in it, the body jsp displayed but not the left menu, top menu and etc.

    Why other jsp are not getting displayed?
    —Struts.xml———————————————–

    myTest
    Error

    ————————————————————
    Tiles.xml

    Reply
  65. vd greg


    ---Struts.xml-----------------------------------------------

    myTest
    Error

    ------------------------------------------------------------
    Tiles.xml

    Reply
  66. vd greg

    —Struts.xml———————————————–

    package name=”vd” namespace=”/vd” extends=”something”
    action name=”vdTest” class=”vdAction”
    result type=”tiles” myTest result
    result name=”error” type=”tiles” Error result
    action>
    package
    ————————————————————
    Tiles.xml
    definition name=”default” template=”/WEB-INF/jsp/default.jsp”
    put-attribute name=”title” value=”Management System”
    put-attribute name=”header” value=”/html/header.html”
    put-attribute name=”menu” value=”/WEB-INF/jsp/menu.jsp”
    put-attribute name=”footer” value=”/html/footer.html”
    put-attribute name=”body” value=”/html/main.html”
    definition>
    definition name=”myTest” extends=”default”
    put-attribute name=”body” value=”/WEB-INF/jsp/vdTest.jsp”
    definition

    Reply
  67. Karel Alfonso

    Hi,

    I’m trying to test Struts actions following this example and I’m getting type cast exception in the line:

    PersonAction action = createAction(PersonAction.class, “/site”, “deletePerson”);

    Exception message says “ActionSupport” cannot be cast to PersonAction.

    Reply
  68. Karel Alfonso

    Hi everyone,

    I’m also running “testInterceptorsBySettingRequestParameters()” with no success. I’m using ognl expressions in the request parameter like “person.name”, “person.description” and the action is getting a null reference to the Person object.

    I have the getter and setter for the person object. While debugging I found the request parameters are null.

    Any help will be very much appreciated,

    Thanks in advance

    Reply
  69. Karel Alfonso

    Hi,

    The class in fact extends ActionSupport and I’m still getting the error.

    The other thing is that the Person object doesn’t get populated in the PersonAction class after setting request parameter “person.name” and “person.description” parameters in the request.

    I’m following this example line by line with the modification of having a Person object that gets populated from the request in the Action bean.

    Thanks for your quick feedback.

    Cheers,

    Karel

    Reply
  70. Mike

    Karen – make sure you have the proper interceptors in your stack. This example is working fine for me using the default struts.xml configuration. Maybe its time to whip out the debugger and see what interceptors are being invoked.

    Reply
  71. Dusty

    So, I am seeing the same issue with MockHttpServletRequest parameters not making it through the Parameters interceptor. I have traced the code pretty deep and it looks like the InvocationContext does not get a copy of the MockHttpServletRequest parameters. There is some interesting comments in ActionContext for setParameters(Map param), where it says “in a Servlet Environment this returns the HttpRequest parameters, otherwise it returns a Map” (paraphrase). So I trying to figure out where this all gets determined. My guess is the Dispatcher so I am headed in that direction.

    While I am doing that, it sounds like people are getting this (request.addParameter(…)) to work. Mike, what version of Struts are you using? What plugins? How are you executing your unit tests? Anything else about your Struts/Testing environment that is not likely to be shared by others?

    Reply
  72. Dusty

    Ok, so I did not figure out why the parameters are not getting copied from the ServletRequest to the InvocationContext, but I have a workaround I think.

    Try replacing:
    request.addParameter(“id”,”1″)
    with
    Map params = new HashMap();
    params.put(“id”,”1″);
    proxy.getInvocation().getInvocationContext().setParameters(params);

    Reply
  73. vidu

    java.lang.NullPointerException at org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:104)at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)


    – at
    vms.junit.test.LoginActionTest.testInterceptorsBySettingDomainObjects(LoginActionTest.java:29)

    My LoginActionTest is——————-
    package vms.junit.test;
    import java.util.Map;
    import vms.action.LoginAction;
    import vms.model.User;
    public class LoginActionTest extends BaseStrutsTestCase {

    public LoginActionTest(String name) {
    super(name);
    }
    public void testInterceptorsBySettingDomainObjects() throws Exception {
    LoginAction action = createAction(LoginAction.class,
    “/”, “Login”);
    User user = new User();
    user.setFirstName(“vidit”);
    user.setLastName(“kumar”);
    action.setUser(user);
    String result = proxy.execute();
    assertEquals(result, “success”);
    }

    }

    I have tiles listener in my web.xml…I dont know why I am getting this error.

    Any help will be much appreciated.

    Reply
  74. Arsenalist Post author

    Some people have reported success using that with Tiles, others have not. The issue here is that the tiles config isn’t being read because it can’t find it in the classpath…

    Sorry, I stopped using Tiles a long time ago in favor of Sitemesh.

    Reply
  75. vidu

    Hi Arsenalist

    thanks for the quick reply. I am loading tiles config in the same way I am loading application-Context and dataAccessContext
    private static final String CONFIG_LOCATIONS = “classpath:/WEB-INF/applicationContext.xml”
    + “, classpath:/WEB-INF/dataAccessContext.xml”
    + “, classpath:/WEB-INF/tiles.xml ” ;

    Is there any other way to load tiles config file.

    Reply
  76. Ezgi Çiçek

    I have a problem with applicationContext.xml file’s configuration. My applicationContext.xml file is not under META-INF, so I change this part :
    private static final String CONFIG_LOCATIONS = “META-INF/applicationContext-app.xml,” +”META-INF/applicationContext-security.xml”;

    to
    private static final String CONFIG_LOCATIONS = “WEB-INF/applicationContext.xml”;

    The error is :

    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] at

    Moreover, I don’t have a file named applicationContext-security.xml.
    I am sure that applicationContext.xml is in the correct place, so what is the problem? Should I do some change on it?

    The applicationContext.xml file :

    Reply
  77. Ezgi Çiçek

    I have solved my problem :)
    I just wrote :
    private static final String CONFIG_LOCATIONS = “file:WebContent/WEB-INF/applicationContext.xml”;
    But I couldn’t understand the difference.

    Reply
  78. Ezgi Çiçek

    After I have solve the configuration problem, there is another problem about tiles. As some peope mentioned that the tiles integration causes NPE, I have the same problem, ant the trace is the same.. I have set the configuration file to

    private static final String CONFIG_LOCATIONS = “file:src/struts.xml,file:WebContent/WEB-INF/tiles.xml” ; this, as suggested, but it did not work. How can I solve this problem ?

    I do not have applicationContext file, I use struts.xml instead of it.

    struts.xml :

    tvq.NameCollector

    tvq.ListTasks

    /error.jsp

    tvq.addTask

    web.xml :

    TVQ

    struts2

    org.apache.struts2.dispatcher.FilterDispatcher

    struts2
    /*

    org.springframework.web.context.ContextLoaderListener

    Tiles_Filter

    org.apache.tiles.web.startup.TilesFilter

    org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG

    /WEB-INF/tiles.xml

    Tiles_Filter
    /*
    REQUEST

    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp

    Reply
  79. Girih

    I have modified BaseStrutsTestCase.createAction to include request and response objects

    protected T createAction(Class clazz,String namespace, String name, MockHttpServletRequest request,MockHttpServletResponse response)

    Modified the following lines in the method

    Map extraContext = dispatcher.createContextMap(request, response, null, servletContext);

    ActionProxy proxy = dispatcher.getContainer().getInstance(
    ActionProxyFactory.class).createActionProxy(namespace, name,
    extraContext, true, false);
    dropped these lines

    proxy.getInvocation().getInvocationContext().
    setParameters(new HashMap());
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    ServletActionContext.setRequest(request);
    ServletActionContext.setResponse(response);

    This will make sure whatever parameter is added to servlet request using addParameter method will end up in the Action domain objects.
    I hope this solves Dusty’s problem :)

    Reply
  80. cat7

    I found a similar code sample to this at http://www.soft-gems.net/index.php?option=com_content&task=view&id=48, which included tiles support. I found that by adding the tiles config from the other example to the code on this page I stopped getting NPE’s in Tiles, although I’m still working through my tests to see if it breaks anything else.

    To add tiles support, find this line in the existing code (currently line 80 in the listing above but it might change) :-

    new JspSupportServlet().init(new MockServletConfig(servletContext));

    and add the following code to define the location of your tiles.xml and sets up a Tile listener

    servletContext.addInitParameter(BasicTilesContainer.DEFINITIONS_CONFIG, “WEB-INF/tiles.xml”);
    final StrutsTilesListener tilesListener = new StrutsTilesListener();
    final ServletContextEvent event = new ServletContextEvent(servletContext);
    tilesListener.contextInitialized(event);

    I cant claim any credit for the above, other than finding it and posting it here :-)

    Reply
    1. Lakshmanan Venkatach

      Thanks for posting it. I was breaking my head on why tiles not getting initialized. Tried several configuration option in struts2 but nothing worked.

      I am thinking of posting the same in several other places where people posted this issue. I saw several people had problems on the same lines.

      Thanks.

      Reply
      1. Sudheer

        Why we need to load tiles.xml for JUnit, are you trying to execute results? This is to know the requirement.

        Thanks

  81. rchak

    We are trying to use this to test action classes in an Struts/Spring/Hibernate application.

    DriverManagerDataSource and LocalSessionFactoryBean are used.

    I am receiving HibernateException that there is no active transaction. Any idea?

    Reply
  82. Ezgi Çiçek

    cat7,

    I tried what you have suggested but It gave an error as I mentioned before:

    org.apache.tiles.definition.NoSuchDefinitionException: tvq.ListTasks

    What can be the problem?

    Reply
  83. simon.void

    Line 57-58
    //do not execute the result after executing the action
    proxy.setExecuteResult(true);

    there is a bug inside this code :-)

    Reply
  84. simon.void

    hi,

    my StrutsActionTestCase is virtually a copy of your BaseStrutsTestCase. I use it first in TestLogin where everything works fine and than in TestRegister where this strange assertion error is thrown:

    java.lang.AssertionError:
    Unexpected method call put(“configurationReload-struts-default.xml”, true):
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:56)
    at org.easymock.classextension.internal.ClassProxyFactory$1.intercept(ClassProxyFactory.java:74)
    at com.opensymphony.xwork2.ActionContext$$EnhancerByCGLIB$$8aa46a90.put()
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:110)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
    at test.actions.StrutsActionTestCase.setUp(StrutsActionTestCase.java:94)
    at test.actions.security.TestRegister.setUp(TestRegister.java:27)

    StrutsActionTestCase.java:94 coresponds to BaseStrutsTestCase.java:90 which is the line with
    dispatcher.init().
    Why is the ActionContext an EasyMock-Mock-class???

    Reply
  85. simon.void

    the quickfix seems to be to reset the ActionContext in BaseStrutsTestCase.setUp() between line 71 and line 72:
    protected void setUp() throws Exception
    {
    ActionContext.setContext( new ActionContext(new TreeMap()) );
    if( applicationContext == null ) {
    ….

    But there seems to be a bug somewhere!?

    Reply
  86. Pingback: 2008 Voluntary Summer Internship « Just give it a try!

  87. vd greg

    I am trying to unit test one action class which returns sucess…
    I am getting this error…

    -Info — Initializing Tiles2 container. . .
    -WARN [org.apache.tiles.impl.BasicTilesContainer] – Unable to find configured definition ‘/WEB-INF/tiles.xml’

    2008-08-07 13:51:19,583 INFO [org.apache.tiles.impl.BasicTilesContainer] – Tiles2 container initialization complete.

    Reply
  88. Miguel

    I’m having the same Classpath problems that some have found (46).

    On my setup, I have a unittests source folder (separate from both the src and WebContent).

    String CONFIG_LOCATIONS = “WEB-INF/applicationContext.xml”;
    doesn’t find the file (nor tiles.xml, for example). “WebContent/WEB-INF/…” doesn’t find it either.

    I’ve also tried the full system path without any luck.

    Everything works great if I copy-paste the xml files to the unittests source folder. But how can I reference the files in the WEB-INF folder?

    Reply
  89. Anand

    I’m new to this these technologies. so while I copied this class to run in my eclipse it is giving me 2 errors which is in “T” -no class exit..

    how to resolve this.

    Reply
  90. nodje

    I’ve been successfully using your BaseStrutsTestCase under TestNG with Struts-2.1.2 and Spring-2.5.6.
    Pretty nice, thanks!

    I was wondering how you handle the fact the service layer problem. In order to test your actions, you need to provide some persistent entity, right? If you want to keep away from the container, you’ve got to mock the Service layer in some ways so as to be able to return expected entity for your action.
    How do you mock them? Do you use a specific tool?

    Reply
  91. Arsenalist Post author

    My DAOs implement interfaces and I have a “real” implementation and a test implementation which I use during testing. I use XStream and a bunch of XML files to mock data coming from a database during testing.

    Reply
  92. Sudheer

    Nice article, I tried the same but facing following issue when I try to run the JUnits

    [junit] Running com.pg.site.actions.about.AboutUsActionTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Test com.pg.site.actions.about.AboutUsActionTest FAILED (crashed)

    The message in the log is

    junit.framework.AssertionFailedError: Forked Java VM exited abnormally. Please note the time in the report does not reflect the time until the VM exit.

    If any of you faced such issue, can you please help me.

    Reply
  93. Niray

    I implemented SessionAware interface in Action class and i just set the session using,
    ….

    ServletActionContext.getContext().setSession(session);
    ServletActionContext.setServletContext(servletContext);
    ……
    and it works like magic

    Reply
  94. Incomp1337

    Hi Arsenalist, nice job!
    I’ve implemented your framework and it’s been incredibly helpful. One question though: I’ve not been able to get it to add request parameters using your method in the example (request.addParameter(“id”, “123”)). The values just don’t get noticed by the interceptors. So, I’ve been doing it using the method that the base action uses (proxy.getInvocation().getInvocationContext().getParameters().put( …. ) ).
    That works well, but it’s a little messy, because that particular parameters map requires all parameter values to be String[]s, not Strings, so i have to add extra lines into the code. Any idea what I might be doing wrong?
    Cheers.

    Reply
  95. Incomp1337

    Just an extra note on my last post: I tried Girih’s method from 7/2/08, but I wasn’t able to get it work…

    Reply
  96. dilip

    In the method definition:
    protected T createAction(Class clazz, String namespace, String name)

    For the class ‘T’ should i import the following class
    import org.apache.poi.hssf.record.formula.functions.T;

    Reply
  97. dilip

    In the method definition:
    protected T createAction(Class clazz, String namespace, String name)

    For the class ‘T’ should i import the following class
    import org.apache.poi.hssf.record.formula.functions.T;

    is this correct import?

    Reply
  98. Joaquin Diez

    Hi

    I am trying to use your framework and is really usefull, in my case I am using struts2-rest-struts-plugin, and it use RestActionProxyFactory instead of ActionProxyFactory

    I modify the code..like follow but I get a null action Proxy.. any ideas?

    RestActionProxyFactory actionProxy = dispatcher.getContainer().getInstance(RestActionProxyFactory.class, “rest”);
    proxy = actionProxy.createActionProxy(namespace, name, null, true, false);

    Reply
  99. Jorgan

    Got an error with:

    private static final String CONFIG_LOCATIONS = “file:WebContent/WEB-INF/applicationContext.xml”;

    at
    applicationContext = (new ContextLoader()).initWebApplicationContext(servletContext);

    with:

    Caused by: java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.properties()[Ljavax/persistence/PersistenceProperty;

    Can someone refer to the xml documents? is the security.xml necessary? sorry i am kinda new to this

    Thx, Jorgan

    Reply
    1. Sudheer

      You can load xml documents, but ensure you get the absolute path or relative path from the classpath to load them.

      Reply
  100. Frank

    Hi,

    It’s very helpful. But I’ve an problem.

    I’ve an action implement Preparable. When I run the proxy.execute(), the EntityManager is opened/closed twice. This causes a problem that an entity feched in prepare() becomes detached.

    Any solution?

    Thanks,

    Reply
  101. stanlick

    For those of you who are using this base class to unit test your Struts 2 apps, have you encountered this requirement?

    I have a web.xml listener placing an object in application scope using servletContext.setAttribute(foo,engine) and an interceptor expecting to locate it using actionInvocation.getStack().findValue(“#attr.foo”).

    In my unit test which subclasses BaseStrutsTestCase I am putting the engine in scope using proxy.getInvocation().getStack().set(“foo”, engine), however my interceptor is finding a null when it attempts to fetch it from the stack. Is there something weird concerning the OgnlValueStack in this context?

    Peace,
    Scott

    Reply
  102. dc

    I was wondering if the problem with the aware interface (SessionAware) as described in comments 24 & 25 was ever resolved.

    I have recently started experimenting with this very helpful unit testing code and facing same issue.

    Looks like the framework isn’t setting a session object to my class implementing SessionAware. So, I call the setSession() method to set it myself. That happens before proxy.execute(), but when the time comes for proxy.execute(0 to run, it resets the session map to null.

    The test passes with the option to by-pass the framework and test the action in an isolated function.

    Not sure if this discussion is still active, but if anyone has any advice or solution on this issue, I would very interested in hearing it.

    Many thanks.

    Reply
  103. Bruce

    Zarar – I modified your BaseStrutsTestCase class so that it works for testing a Struts 2 Action class that doesn’t use Spring. I’d like to publish this code along with an example of how to use it on my blog (http://www.brucephillips.name/blog). I’ve cited your name and blog article in the code comments and also will cite your orignal work in the blog article.

    Is is OK with you if I publish on my blog the modified BaseStrutsTestCase class I created based on your work?

    Bruce Phillips

    Reply
  104. Arindam Ray

    I am getting the following error:

    java.lang.NullPointerException
    at org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:104)
    at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    at gc.test.unittest.testaction.TestLoginActionUsingBaseStrutsTestCase.testInterceptorsBySettingRequestParameters(TestLoginActionUsingBaseStrutsTestCase.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:91)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

    Reply
    1. Arindam Ray

      I have got the answer.

      Complete BaseStrutsTestCase is like following:

      import com.opensymphony.xwork2.ActionContext;
      import com.opensymphony.xwork2.ActionProxy;
      import com.opensymphony.xwork2.ActionProxyFactory;
      import junit.framework.TestCase;
      import org.apache.struts2.ServletActionContext;
      import org.apache.struts2.dispatcher.Dispatcher;
      import org.apache.struts2.tiles.StrutsTilesListener;
      import org.apache.struts2.views.JspSupportServlet;
      import org.apache.tiles.impl.BasicTilesContainer;
      import org.springframework.context.ApplicationContext;
      import org.springframework.mock.web.MockHttpServletRequest;
      import org.springframework.mock.web.MockHttpServletResponse;
      import org.springframework.mock.web.MockServletConfig;
      import org.springframework.mock.web.MockServletContext;
      import org.springframework.web.context.ContextLoader;

      import java.util.HashMap;
      import java.util.Map;

      import javax.servlet.ServletContextEvent;

      /**
      * @author Arindam Ray
      */
      @SuppressWarnings(“unchecked”)
      public abstract class BaseStrutsTestCase extends TestCase {
      private static final String CONTEXT_CONFIG_LOCATION = “/WEB-INF/applicationContext.xml,” +
      “/WEB-INF/dataAccessContext.xml”;

      private static final String TILES_DEFINITIONS = “/WEB-INF/tiles.xml”;

      private static ApplicationContext applicationContext;
      private Dispatcher dispatcher;
      protected ActionProxy proxy;
      protected static MockServletContext servletContext;
      protected MockHttpServletRequest request;
      protected MockHttpServletResponse response;

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

      /**
      * Created action class based on namespace and name
      * @param clazz Class for which to create Action
      * @param namespace Namespace of action
      * @param name Action name
      * @param parameters Request parameters
      * @param session Session map
      * @return Action class
      * @throws Exception Catch-all exception
      */
      protected Object createAction(Class clazz, String namespace, String name, Map parameters, Map session)
      throws Exception {

      // create a proxy class which is just a wrapper around the action call.
      // The proxy is created by checking the namespace and name against the
      // struts.xml configuration
      proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(namespace, name, null, true, false);

      ActionContext actionContext = proxy.getInvocation().getInvocationContext();

      if(parameters == null) {
      // by default, don’t pass in any request parameters
      actionContext.setParameters(new HashMap());
      }
      else {
      //set the parameters
      actionContext.setParameters(parameters);
      }

      if(session == null) {
      actionContext.setSession(new HashMap());
      }
      else {
      //set the session
      actionContext.setSession(session);
      }

      // do not execute the result after executing the action
      proxy.setExecuteResult(true);

      // set the actions context to the one which the proxy is using
      ServletActionContext.setContext(actionContext);
      request = new MockHttpServletRequest();
      response = new MockHttpServletResponse();
      ServletActionContext.setRequest(request);
      ServletActionContext.setResponse(response);
      ServletActionContext.setServletContext(servletContext);
      return proxy.getAction();
      }

      protected void setUp() throws Exception {
      if( applicationContext == null ) {
      // this is the first time so initialize Spring context
      servletContext = new MockServletContext();
      servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, CONTEXT_CONFIG_LOCATION);
      applicationContext = (new ContextLoader()).initWebApplicationContext(servletContext);

      // Struts JSP support servlet (for Freemarker)
      new JspSupportServlet().init(new MockServletConfig(servletContext));

      //for tiles
      servletContext.addInitParameter(BasicTilesContainer.DEFINITIONS_CONFIG, TILES_DEFINITIONS);
      final StrutsTilesListener tilesListener = new StrutsTilesListener();
      final ServletContextEvent event = new ServletContextEvent(servletContext);
      tilesListener.contextInitialized(event);
      }
      // Dispatcher is the guy that actually handles all requests. Pass in
      // an empty. Map as the parameters but if you want to change stuff like
      // what config files to read, you need to specify them here. Here’s how to
      // scan packages for actions (thanks to Hardy Ferentschik – Comment 66)
      // (see Dispatcher’s source code)
      HashMap params = new HashMap();
      params.put(“actionPackages”, “gc.action”);
      dispatcher = new Dispatcher(servletContext, params);
      dispatcher.init();
      Dispatcher.setInstance(dispatcher);
      }
      }

      Reply
  105. Dipesh Jethwa

    Thanks Arsenalist for the code.

    Has anybody got the above code to run by setting request parameters? The method testInterceptorsBySettingRequestParameters() doesnot work for me. There were quite few queries on this. Has anyone got it to work?

    Reply
  106. shanggui

    Thank for your post! Great work!
    One question is: when I run testInterceptorsBySettingDomainObjects(), it works, but can
    not invoke the interceptor. I’m using struts2+spring2+tiles

    Thank You!

    Reply
  107. Steven Bromley

    After a few hours of debugging I got this code to work perfectly! Thank you so much for not only posting the code, but revising it and helping with other users. This post makes it easy to set this up and troubleshoot.

    Reply
  108. Dan

    A small change is needed for Struts 2.1.6. The createActionProxy() method of ActionProxyFactory needs an additional parameter for the action’s ‘method’.

    To set the Action’s domain objects through request parameter, the suggested approach will not work. i.e.
    request.setParameter(“id”, “123”) will not set the Id field of the action class. That has to be done in the following way –
    Map params = new HashMap();
    params.put(“id”, “123”);
    proxy.getInvocation().getInvocationContext().setParameters(params);

    Reply
  109. shreyas

    how to solve this problem

    There is no Action mapped for namespace / and action name createaccount. – [unknown location]
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:177)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
    at net.action.StrutsTestCase.getActionProxy(StrutsTestCase.java:84)
    at net.action.NewTest.testUserName(NewTest.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    Reply
  110. shreyas

    this one is very helpful example
    how to solve this problem

    There is no Action mapped for namespace / and action name createaccount. – [unknown location]
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:177)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
    at net.action.StrutsTestCase.getActionProxy(StrutsTestCase.java:84)
    at net.action.NewTest.testUserName(NewTest.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    Reply
  111. alex

    There was something wrong with the usage of “assertEquals(result, “success”);”

    The actual API is: assertEquals(Object expected, Object actual)

    Reply
  112. after school care|

    Unquestionably believe that which you said.

    Your favorite reason seemed to be on the web the easiest factor to take into accout
    of. I say to you, I definitely get annoyed while folks think about worries that
    they plainly don’t recognise about. You managed to hit the nail upon the highest and defined out the whole thing without having side effect , people could take a signal. Will likely be again to get more. Thanks

    Reply
  113. Pingback: Struts2 Convention & Spring Junit4 | Code you need

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s