• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Struts2: Test action classes

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Author
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Does it work in the app, just going to "manageSamples", with no underscore or method?
     
    Nick Sher
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 78
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 78
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What happens if you put the struts.xml at the root of the classpath?
     
    Nick Sher
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 78
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Still not able to set parameters on the action.
     
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic