# Unit Testing Struts 2 Actions wired with Spring using JUnit

## 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:

 01 public class PersonAction extend ActionSupport {

 02 

 03   private int id;

 04 

 05   public int getId() {

 06     return id;

 07   }

 08   public void setId(int id) {

 09     this.id = id;

 10   }

 11   public String deletePerson() {

 12     ....

 13     return SUCCESS;

 14   }

 15 }

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.

 01 import com.opensymphony.xwork2.ActionProxy;

 02 import com.opensymphony.xwork2.ActionProxyFactory;

 03 import junit.framework.TestCase;

 04 import org.apache.struts2.ServletActionContext;

 05 import org.apache.struts2.dispatcher.Dispatcher;

 06 import org.apache.struts2.views.JspSupportServlet;

 07 import org.springframework.context.ApplicationContext;

 08 import org.springframework.mock.web.MockHttpServletRequest;

 09 import org.springframework.mock.web.MockHttpServletResponse;

 10 import org.springframework.mock.web.MockServletConfig;

 11 import org.springframework.mock.web.MockServletContext;

 12 import org.springframework.web.context.ContextLoader;

 13 

 14 import java.util.HashMap;

 15 

 16 /**

 17  * @author Zarar Siddiqi

 18  */

 19 public abstract class BaseStrutsTestCase extends TestCase {

 20     private static final String CONFIG_LOCATIONS = "META-INF/applicationContext-app.xml," +

 21                 "META-INF/applicationContext-security.xml";

 22     private static ApplicationContext applicationContext;

 23     private Dispatcher dispatcher;

 24     protected ActionProxy proxy;

 25     protected static MockServletContext servletContext;

 26     protected static MockServletConfig servletConfig;

 27     protected MockHttpServletRequest request;

 28     protected MockHttpServletResponse response;

 29 

 30     public BaseStrutsTestCase(String name) {

 31         super(name);

 32     }

 33 

 34     /**

 35      * Created action class based on namespace and name

 36      * @param clazz Class for which to create Action

 37      * @param namespace Namespace of action

 38      * @param name Action name

 39      * @return Action class

 40      * @throws Exception Catch-all exception

 41      */

 42     @SuppressWarnings("unchecked")

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

 44             throws Exception {

 45 

 46         // create a proxy class which is just a wrapper around the action call.

 47         // The proxy is created by checking the namespace and name against the

 48         // struts.xml configuration

 49         proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class).

 50                 createActionProxy(

 51                 namespace, name, null, true, false);

 52 

 53         // by default, don't pass in any request parameters

 54         proxy.getInvocation().getInvocationContext().

 55                 setParameters(new HashMap());

 56 

 57         // do not execute the result after executing the action

 58         proxy.setExecuteResult(true);

 59 

 60         // set the actions context to the one which the proxy is using

 61         ServletActionContext.setContext(

 62                 proxy.getInvocation().getInvocationContext());

 63         request = new MockHttpServletRequest();

 64         response = new MockHttpServletResponse();

 65         ServletActionContext.setRequest(request);

 66         ServletActionContext.setResponse(response);

 67         ServletActionContext.setServletContext(servletContext);

 68         return (T) proxy.getAction();

 69     }

 70 

 71     protected void setUp() throws Exception {

 72         if( applicationContext == null ) {

 73             // this is the first time so initialize Spring context

 74             servletContext = new MockServletContext();

 75             servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,

 76                     CONFIG_LOCATIONS);

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

 78 

 79             // Struts JSP support servlet (for Freemarker)

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

 81         }

 82         // Dispatcher is the guy that actually handles all requests.  Pass in

 83         // an empty. Map as the parameters but if you want to change stuff like

 84         // what config files to read, you need to specify them here.  Here's how to

 85         // scan packages for actions (thanks to Hardy Ferentschik - Comment 66)

 86         // (see Dispatcher's source code)

 87         HashMap params = new HashMap();

 88         params.put("actionPackages", "com.arsenalist.action");

 89         dispatcher = new Dispatcher(servletContext, params);

 90         dispatcher.init();

 91         Dispatcher.setInstance(dispatcher);

 92     }

 93 }

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.
 01 public class PersonActionTest extends BaseStrutsTestCase {

 02 

 03  /**

 04   * Invoke all interceptors and specify value of the action

 05   * class' domain objects directly.

 06   * @throws Exception Exception

 07   */

 08  public void testInterceptorsBySettingDomainObjects()

 09          throws Exception {

 10   PersonAction action = createAction(PersonAction.class,

 11                 "/site", "deletePerson");

 12   action.setId(123);

 13   String result = proxy.execute();

 14   assertEquals(result, "success");

 15  }

 16 

 17  /**

 18   * Invoke all interceptors and specify value of action class'

 19   * domain objects through request parameters.

 20   * @throws Exception Exception

 21   */

 22  public void testInterceptorsBySettingRequestParameters()

 23                      throws Exception {

 24   createAction(PersonAction.class, "/site", "deletePerson");

 25   request.addParameter("id", "123");

 26   String result = proxy.execute();

 27   assertEquals(result, "success");

 28  }

 29 

 30  /**

 31   * Skip interceptors and specify value of action class'

 32   * domain objects by setting them directly.

 33   * @throws Exception Exception

 34   */

 35  public void testActionAndSkipInterceptors() throws Exception {

 36   PersonAction action = createAction(PersonAction.class,

 37                   "/site", "deletePerson");

 38   action.setId(123);

 39   String result = action.deletePerson();

 40   assertEquals(result, "success");

 41  }

 42 }

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:

 1 public void testValidation() throws Exception {

 2  SomeAction action = createAction(SomeAction.class,

 3                  "/site", "someAction");

 4  // lets forget to set a required field: action.setId(123);

 5  String result = proxy.execute();

 6  assertEquals(result, "input");

 7  assertTrue("Must have one field error",

 8                  action.getFieldErrors().size() == 1);

 9 }

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.

1. Yin

Thanks for the code, it works nicely.

2. Posted June 21, 2007 at 8:18 am | Permalink | Reply

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

3. Francisco Rosa

Pretty nice. I do believe this is one of the places where Struts 2.0 could offer a lot more developer support. I have been tackling this issue for sometime and written also some utility classes to simplify Struts 2.0 + Spring unit testing…
Here’s my take on it:

http://fassisrosa.blogspot.com/2007/03/unit-testing-struts-20-part-2.html

4. 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);
//…
}

5. Posted July 4, 2007 at 5:13 am | Permalink | Reply

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)?

6. Posted July 4, 2007 at 9:38 am | Permalink | Reply

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…

• Arindam Ray

7. 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.

8. 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.

9. Posted August 5, 2007 at 4:21 pm | Permalink | Reply

I’m using Struts2 without Spring. And I would like to know how can I do a junit test using struts2??

10. Devarajan

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

11. 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)

Thanks and great work!

12. 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

• 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();

13. 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();

14. Posted August 30, 2007 at 9:52 am | Permalink | Reply

Hi Scott, that should read proxy.execute(), not invoke(). I’ve fixed the example. Thanks for pointing out the error.

15. Scott

Sweet! Is there a way to get the model from the action post execute method call?

16. Posted August 30, 2007 at 12:57 pm | Permalink | Reply

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..

17. 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

18. Scott

My bad! I did not have Spring injecting the model. Sorry if you looked into this already.

Peace,
Scott

19. Scott

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

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

20. Posted August 31, 2007 at 10:28 am | Permalink | Reply

Yup, only reason. Just to avoid a cast in the code that calls createAction(), it’s a little neater (I think).

21. 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.

22. 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

23. Posted September 2, 2007 at 2:21 pm | Permalink | Reply

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.

24. 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

25. Scott

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

(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

26. 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?

27. Posted September 10, 2007 at 8:35 am | Permalink | Reply

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().

28. 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.

29. Posted October 12, 2007 at 8:52 pm | Permalink | Reply

Do you have the link to the source code?

30. Posted October 12, 2007 at 8:54 pm | Permalink | Reply

It doesn’t seem to work with Struts 2.0.9. Thanks really.

31. Posted October 13, 2007 at 10:02 am | Permalink | Reply

It’s working fine with 2.0.9 for me, might want to post the error you’re getting.

32. 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?

33. Posted November 4, 2007 at 9:35 am | Permalink | Reply

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)

34. Posted November 4, 2007 at 4:00 pm | Permalink | Reply

I’ve updated the source code to make it a little simpler, you don’t need StrutsSpringObjectFactory anymore. Try it again and let me know.

35. Posted November 6, 2007 at 9:25 pm | Permalink | Reply

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.

36. Mike

What changes would need to be made if I’m not using Spring? Thanks guys, tons of help!

37. 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

• Arindam Ray

I am also having the same problem…Can anyone pls help me?…

38. 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

39. Posted November 15, 2007 at 10:53 pm | Permalink | Reply

Thanks Felix, I’ve fixed the typo.

40. 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

41. 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?

42. 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

43. 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

44. Posted January 4, 2008 at 9:55 am | Permalink | Reply

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.

• Arindam Ray

can you pls tell me what to do when action returns a tiles type result??

45. 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)

• Anand

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

46. 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 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}

47. 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.

48. dalto

Hi,

I’m very new to java, struts, spring, etc..
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() - startweb-15:23:54,984 DEBUG com.aix2.test.BaseStrutsTestCase:88 - createAction(Class, String, String) - Erro ao criar ActionProxy: java.lang.NullPointerExceptionweb-15:23:54,984 ERROR com.aix2.test.BaseStrutsTestCase:90 - createAction(Class, String, String)java.lang.NullPointerExceptionat 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. • Aaron Posted December 12, 2009 at 5:55 am | Permalink | Reply use like this: CONFIG_LOCATIONS = “file:D:\\开发中项目\\dsunit\\WebRoot\\WEB-INF\\classes\\applicationContext.xml”; Hope you success. 49. Arsenalist Posted January 7, 2008 at 12:47 pm | Permalink | Reply 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. 50. Sri Posted January 7, 2008 at 2:08 pm | Permalink | Reply Hi arsenalist, appreciate your inputs on issue 46 pls. 51. Arsenalist Posted January 7, 2008 at 2:13 pm | Permalink | Reply 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. 52. Sri Posted January 7, 2008 at 2:20 pm | Permalink | Reply 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.

53. Sri

applicationContext

struts entry

/home.action
/changePassword.action?user=${user.userId} 54. Sri Posted January 7, 2008 at 2:21 pm | Permalink | Reply am not sure why my application entry and struts entry is cropped 55. Arsenalist Posted January 7, 2008 at 2:32 pm | Permalink | Reply 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. 56. dalto Posted January 8, 2008 at 6:47 am | Permalink | Reply 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? 57. dalto Posted January 8, 2008 at 6:58 am | Permalink | Reply i’ve added the MockServletContext package directory to the classpath, keeps throwing the same error in the setUp() method. 58. dalto Posted January 8, 2008 at 7:45 am | Permalink | Reply i solved it by referencing the file this way: file///c:/workpath/project/WEB-INF/applicationContext.xml nice article, btw 59. Sri Posted January 8, 2008 at 3:19 pm | Permalink | Reply 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. 60. David Clarke Posted January 10, 2008 at 10:53 pm | Permalink | Reply 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. 61. Posted January 11, 2008 at 10:09 am | Permalink | Reply David, what’s the problem? 62. David Clarke Posted January 13, 2008 at 2:48 pm | Permalink | Reply Well, I would like to know how to enable your BaseStrutsTestCase class to work with action/result annotations. 63. Mark Posted January 15, 2008 at 5:49 am | Permalink | Reply 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. 64. Mohammed Posted January 15, 2008 at 7:44 am | Permalink | Reply 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

65. 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.

66. Posted January 25, 2008 at 11:36 am | Permalink | Reply

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

67. Posted January 25, 2008 at 11:57 am | Permalink | Reply

Thank you Hardy for that! I’ve updated the post.

68. David Clarke

Thanks Hardy, that would be worth a woot! Your timing is impeccable.

69. Posted January 29, 2008 at 3:47 pm | Permalink | Reply

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.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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 70. felix Posted January 30, 2008 at 12:35 am | Permalink | Reply 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 71. Andrew Posted January 31, 2008 at 1:27 pm | Permalink | Reply 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! 72. Andrew Posted January 31, 2008 at 2:00 pm | Permalink | Reply 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. 73. mani Posted February 11, 2008 at 3:21 am | Permalink | Reply good 74. Posted February 24, 2008 at 6:23 pm | Permalink | Reply 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! 75. Jignesh Posted March 3, 2008 at 11:36 pm | Permalink | Reply 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 ? 76. Posted March 5, 2008 at 4:29 am | Permalink | Reply 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”); 77. Posted March 26, 2008 at 1:30 am | Permalink | Reply somebody copied your article word by word here http://itefforts.blogspot.com/2007/08/struts2-spring-junit.html 78. Bigster Posted March 26, 2008 at 6:07 am | Permalink | Reply 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 79. Posted March 26, 2008 at 8:24 am | Permalink | Reply 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); 80. Bigster Posted March 26, 2008 at 9:07 am | Permalink | Reply 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? • Arindam Ray Posted October 6, 2009 at 1:38 pm | Permalink | Reply 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”; 81. Tavo Posted April 3, 2008 at 10:14 am | Permalink | Reply 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. 82. Tavo Posted April 3, 2008 at 3:23 pm | Permalink | Reply 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. 83. vd greg Posted April 7, 2008 at 4:22 pm | Permalink | Reply 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 84. vd greg Posted April 7, 2008 at 4:23 pm | Permalink | Reply 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 85. vd greg Posted April 7, 2008 at 4:24 pm | Permalink | Reply ---Struts.xml----------------------------------------------- myTestError ------------------------------------------------------------Tiles.xml 86. vd greg Posted April 7, 2008 at 4:27 pm | Permalink | Reply —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 87. Karel Alfonso Posted May 25, 2008 at 11:20 pm | Permalink | Reply 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. 88. Karel Alfonso Posted May 25, 2008 at 11:28 pm | Permalink | Reply 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 89. Posted May 27, 2008 at 12:29 am | Permalink | Reply Karel, for #87 make sure your PersonAction class extends ActionSupport. I suspect #88 might be related too. 90. Karel Alfonso Posted May 27, 2008 at 2:17 am | Permalink | Reply 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 91. Mike Posted May 27, 2008 at 1:39 pm | Permalink | Reply 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. 92. Dusty Posted June 2, 2008 at 5:44 pm | Permalink | Reply 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? 93. Dusty Posted June 2, 2008 at 6:43 pm | Permalink | Reply 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); 94. vidu Posted June 9, 2008 at 3:59 pm | Permalink | Reply 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. 95. Posted June 9, 2008 at 5:50 pm | Permalink | Reply 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. 96. vidu Posted June 9, 2008 at 7:10 pm | Permalink | Reply 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. 97. Posted June 23, 2008 at 12:59 pm | Permalink | Reply Some time ago now but… For issues 45 & 64 – the JspTemplateEngine is dependant on the jsp-api.jar Make sure this is available on the class path. 98. Posted June 25, 2008 at 10:40 am | Permalink | Reply 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 : 99. Posted June 25, 2008 at 10:46 am | Permalink | Reply 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. 100. Posted July 1, 2008 at 8:38 am | Permalink | Reply 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 101. Girih Posted July 2, 2008 at 3:57 pm | Permalink | Reply 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 102. Posted July 3, 2008 at 2:36 pm | Permalink | Reply For the ones who want to integrate this unit test with tiles, I have found a tutorial-like post : http://www.soft-gems.net/index.php?option=com_content&task=view&id=48&Itemid=33 However, It gave an exception like that: org.apache.tiles.definition.NoSuchDefinitionException: tvq.ListTasks The program works correctly, but I can not test it 103. cat7 Posted July 4, 2008 at 12:45 pm | Permalink | Reply 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 • Lakshmanan Venkatach Posted May 26, 2009 at 12:41 pm | Permalink | Reply 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. • Sudheer Posted May 26, 2009 at 1:33 pm | Permalink Why we need to load tiles.xml for JUnit, are you trying to execute results? This is to know the requirement. Thanks • Posted December 31, 2010 at 6:24 pm | Permalink | Reply Thank you so much! This is exactly what I’ve been searching for hours. It also serves as a good example to load any other ServletContextListener classes. 104. rchak Posted July 6, 2008 at 11:35 pm | Permalink | Reply 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? 105. Posted July 7, 2008 at 11:11 am | Permalink | Reply 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? 106. simon.void Posted July 15, 2008 at 12:05 pm | Permalink | Reply Line 57-58 //do not execute the result after executing the action proxy.setExecuteResult(true); there is a bug inside this code 107. simon.void Posted July 15, 2008 at 1:04 pm | Permalink | Reply 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 com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
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???

108. 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!?

109. 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.

110. Marcus

PS. session and request are *local* variable. Do not use the member variable of the same name.

111. 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?

112. Ravikanth

Is Junit support Struts 2.0?

I am trying to write Junit Test Case for DAO’s but Objects are not created.

113. Mircea

Thank you for this great post !

114. 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.

115. 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?

116. Posted January 8, 2009 at 11:01 pm | Permalink | Reply

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.

117. Sudheer

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

[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec

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.

118. 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

119. 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.

120. 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…

121. 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;

122. 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?

123. Posted March 23, 2009 at 3:39 pm | Permalink | Reply

T is a reference to any class in Java 1.5 generics. You don’t need to import anything.

124. 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);

125. Jorgan

Got an error with:

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

at

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

• Sudheer

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

126. 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,

127. Posted June 18, 2009 at 7:54 pm | Permalink | Reply

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

128. 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.

129. Posted September 3, 2009 at 9:43 pm | Permalink | Reply

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

130. Posted September 4, 2009 at 8:58 am | Permalink | Reply

Sure Bruce, no problem.

131. 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 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 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) • Arindam Ray Posted October 6, 2009 at 1:41 pm | Permalink | Reply 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); } } 132. Dipesh Jethwa Posted December 18, 2009 at 12:16 pm | Permalink | Reply 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? 133. Posted January 27, 2010 at 3:24 pm | Permalink | Reply I used the approach here until I recently upgraded Struts and came up with an alternative: http://www.digitalsanctum.com/2010/01/25/how-to-test-struts-2-actions-without-a-container/ My approach uses a combination of Struts and Spring to achieve transactional tests without the use of a container. 134. shanggui Posted February 17, 2010 at 6:29 pm | Permalink | Reply 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! 135. Posted June 9, 2010 at 8:14 pm | Permalink | Reply 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. 136. Dan Posted August 6, 2010 at 5:05 pm | Permalink | Reply 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); 137. shreyas Posted January 11, 2011 at 9:35 am | Permalink | Reply 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)

138. Posted January 11, 2011 at 9:37 am | Permalink | Reply

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 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)

139. alex

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

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

1. [...] Later on, I found a wise solution to this problem by searching and trying new possibilities. This blog was a helper for [...]

posted on 2012-02-06 23:20  lexus 阅读( ...) 评论( ...) 编辑 收藏

• 0
点赞
• 0
评论
• 0
收藏
• 扫一扫，分享海报

10-15 8058

01-11 2394
06-29 2139
04-11 5116
06-24 2241
03-09 3158
05-17 1986
10-25 4863
05-20 4853
06-03 3738
07-13 3372
03-03 3088
06-05 6327
03-01 4134

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。