Results 1 to 5 of 5

Thread: Testing Struts Actions

  1. #1
    Join Date
    Aug 2004
    Location
    Wellington, New Zealand
    Posts
    15

    Default Testing Struts Actions

    Hi,

    I am trying to create tests for my Struts Actions using MockStrutsTestCase. I have everything working correctly in that I can test my basic actions without problems. I am using the ContextLoaderPlugin and my action classes are getting injected correctly.

    I am using Hiberate with lazy initialisation and would like to test the results of my Struts actions to ensure the data was populated correctly.

    The problem is that once the action completes, my transaction also completes and my data cannot be initialised.

    In my unit tests for my DAO's I have loaded the ApplicationContext before my tests run, retrieved the session from the context and set my transaction to span the unit test allowing me to test data that is lazy initialised by hibernate.

    I'm trying the same with my struts tests, but I'm not having any success. In my setUp method, I try to load the application context so that I can get a hold of the session, etc. This works fine, but MockStrutsTestCase also loads the application context on the first call to actionPerform (I think). This results in the context being loaded twice, or so it looks from the log files. If it is, a new hibernate session is also being opened and closed for the action so when I test my data, it cannot lazy initialise any data.

    I am looking for a way to load the context and let MockStrutsTestCase know about the context so that it doesn't load it again.

    I have tried the following code that I've seen in some other posts, but this does not help.

    Code:
              MockServletContext sc = new MockServletContext("");
              sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,"/WEB-INF/applicationContext.xml");
              ServletContextListener contextListener = new ContextLoaderListener();
              ServletContextEvent event = new ServletContextEvent(sc);
              contextListener.contextInitialized(event);
    
              // magic bridge to make StrutsTestCase aware of Spring"s Context
              getSession().getServletContext().setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
              ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getSession().getServletContext());
    Am I missing something??

    Thanks
    Bryan

  2. #2
    Join Date
    Aug 2004
    Location
    Wellington, New Zealand
    Posts
    15

    Default

    I think I found the problem that I was having. I'm using the OpenSessionInView filter in web.xml and also using the struts plugin in struts-config.xml.

    It looks like the filter initialises Spring and then the Plugin also initialises Spring so that it is being loaded twice. Once I removed the Plugin, everything worked as expected.

    Is it possible to use the Plugin and the OpenSessionInView filter?

    Thanks,
    Bryan

  3. #3
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    When using the plugin, you may use OpenSessionInViewInterceptor.

    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  4. #4
    Join Date
    Aug 2004
    Location
    Wellington, New Zealand
    Posts
    15

    Default

    Thanks.

    For some reason, I thought the OpenSessionInViewInterceptor was for Spring MVC apps. Not quite sure why I thought that.

    I'll give it a try now.

    Bryan

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Quote Originally Posted by bryanross
    Thanks.

    For some reason, I thought the OpenSessionInViewInterceptor was for Spring MVC apps. Not quite sure why I thought that.

    I'll give it a try now.

    Bryan
    well, OpenSessionInViewInterceptor is for Spring MVC apps.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Spring managed Struts Actions
    By drc in forum Web
    Replies: 2
    Last Post: Jul 14th, 2005, 10:46 AM
  2. Auto-injecting Struts Actions
    By steve_molitor in forum Web
    Replies: 0
    Last Post: Apr 21st, 2005, 04:03 PM
  3. Testing troubles with Struts / Spring
    By anthonyb in forum Web
    Replies: 4
    Last Post: Feb 10th, 2005, 03:44 PM
  4. Replies: 10
    Last Post: Nov 2nd, 2004, 09:38 AM
  5. Replies: 4
    Last Post: Oct 20th, 2004, 05:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •