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.
Am I missing something??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());
Thanks
Bryan


Reply With Quote