aspose file tools*
The moose likes Struts and the fly likes Struts2: Test action classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2: Test action classes" Watch "Struts2: Test action classes" New topic
Author

Struts2: Test action classes

Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
HI,

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)

applicationContext.xml

struts.xml (there are also interceptors in this file)

Test class

Suite


This is not working.

Questions:
  • 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.


  • Please help.


    Nick
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Does it work in the app, just going to "manageSamples", with no underscore or method?
    Nick Sher
    Ranch Hand

    Joined: Nov 10, 2008
    Posts: 78
    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.
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Well yeah, I know what you're trying to do.

    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.
    Nick Sher
    Ranch Hand

    Joined: Nov 10, 2008
    Posts: 78
    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.
    Nick Sher
    Ranch Hand

    Joined: Nov 10, 2008
    Posts: 78
    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
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    What happens if you put the struts.xml at the root of the classpath?
    Nick Sher
    Ranch Hand

    Joined: Nov 10, 2008
    Posts: 78
    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?
  • Nick Sher
    Ranch Hand

    Joined: Nov 10, 2008
    Posts: 78
    Still not able to set parameters on the action.
    James Gadbury
    Greenhorn

    Joined: Mar 20, 2009
    Posts: 24
    Nick Sher wrote:Still not able to set parameters on the action.


    Does this help? http://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/#comment-1127
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Struts2: Test action classes