I have never done unit testing for action classes. Reading the S2A and blogs online (arsenalists) was of little help and I could not run a simple test case. I am struggling with the setup itself. This is what I have as of now and my questions are at the end.
BaseStrutsCase (copied from arsenalists.com)
struts.xml (there are also interceptors in this file)
This is not working.
Is this the correct way of testing action classes?
Error that I get when running this code is "There is no action mapped for 'manageSamples". I think the struts.xml is not found here or for whatever reason action is not initialized. I don't know how struts.xml is detected by this code.
What am I missing and what do I need to do here? My code that is to be tested is in the src folder and the above mentioned code is in the testsrc folder.
David Newton wrote:Does it work in the app, just going to "manageSamples", with no underscore or method?
when the web application is deployed in tomcat, manageSamples_view.action works but managerSamples does not because there is no such mapping. When I run unit tests the app is not deployed. I want to test the code without deploying it.
I didn't see you weren't using the normal StrutsTestCase provided by the framework, so I'd have to dig in to your code to see what all it's doing; this is not likely to happen this week. You should mention what version of S2 you're using, though.
Thanks David for your precious time, your help is greatly appreciated. I'm currently investigating more about Junit to know how I can setup stub for testing struts web applications. I am using Struts 2.1.6. I will do a complete step by step writeup that will help others in the future.
I started digging into StrutsTestCase 2.1.8 to use against Struts 2.1.6 action classes. I am trying out the test cases listed on struts2 web site and I get the same error 'There is no Action mapped for namespace / and action name manageSamples_view'. Here is the test case that is failing:
It fails on line 4. The class extends StrutsSpringTestCase and thats all I have. The struts.xml is located under com.resources package. Any help?
David Newton wrote:What happens if you put the struts.xml at the root of the classpath?
It works! :thumbup: I created a WEB-INF folder moved struts.xml into it and added this folder into classpath. Then I come here to update the message and your reply is already present. Thanks Dave.
Few more questions:
How do I set the properties (request parameters) on the action? I created a new HashMap, added a property to map and then This is not working as I don't see the change in action properties when debugging.
This is more of a Junit question. I inject services into Struts actions as Spring beans (added applicationContext.xml in classpath) and these beans parse some XMLs, which in turn use some .properties files (added .properties files in classpath). Do I need to charge up all these layers when testing the struts actions? or, can I test the layers isolated. If yes then how do I provide the fake services to struts actions?