Hello,

I'm fairly new to Struts 2. I'm currently migrating a project from Struts 1 to Struts 2, but I'd like to make heavy use of Unit Testing to aid in the migration. In the documentation, the testability of the new actions is often mentioned. However, I have the impression that it's not that simple at all.

I put some work into using the StrutsTestCase class as base class for my tests, but I've run into some problems that I can't really understand.

1. There is hardly any documentation regarding the StrutsTestCase or the XWorkTestCase base classes. Not in the documentation, not in the mail archives, not even the issue tracker. Is anyone using these classes at all ? I've found a short part in Ian Roughley's book Starting Struts 2, but the example did not work for me.

2. I did found some examples in the struts2-showcase web application, and building upon these, I succeeded in setting up my test cases. I have my base class, with the setup of request and session objects, the code of which is shown below (picked from the ExecuteAndWaitInterceptorTest class in the showcase source code).

public abstract class BaseTestCase extends StrutsTestCase {

   protected StrutsMockHttpServletRequest request;
   protected HttpSession httpSession;
   protected Map<String, Object> context;
   protected Map<String, Object> params;
   protected Map<String, Object> session;
   protected ServletContext servletContext;
protected void setUp() throws Exception {
       //super.setUp();  <--------- Does not work !!!!!

       loadConfigurationProviders(new LocationConfigurationProvider());
session = new HashMap<String, Object>();
       params = new HashMap<String, Object>();
       context = new HashMap<String, Object>();
       context.put(ActionContext.SESSION, session);
       context.put(ActionContext.PARAMETERS, params);

       request = new StrutsMockHttpServletRequest();
       request.setParameterMap(params);
       context.put(ServletActionContext.HTTP_REQUEST, request);
servletContext = new MockServletContext(); servletContext.setAttribute(DaoPlugIn.DAOMANAGER, DaoConfig.getDaoManager());
       context.put(ServletActionContext.SERVLET_CONTEXT, servletContext);
UserInfo userInfo = ..
       ...
       session.put(AppConstants.APP_USER_INFO_KEY, userInfo);
   }

   // can be overridden by subclasses
   public List<InterceptorMapping> getInterceptors() {
List<InterceptorMapping> interceptors = new ArrayList<InterceptorMapping>(); interceptors.add(new InterceptorMapping("servlet", new ServletConfigInterceptor())); interceptors.add(new InterceptorMapping("params", new ParametersInterceptor())); interceptors.add(new InterceptorMapping("prepare", new PrepareInterceptor())); interceptors.add(new InterceptorMapping("params", new ParametersInterceptor())); return interceptors;
   }
// can be overridden by subclasses
   public Map<String, ResultConfig> getResultConfig() {
Map<String, ResultConfig> results = new HashMap<String, ResultConfig>(); results.put(Action.SUCCESS, new ResultConfig(Action.SUCCESS, MockResult.class.getName(), null)); return results;
   }

   // every test class will test a particular action class
   public abstract Class getActionClass();

// add package configurations, containing action names, method names, ... // this is specific for each test class, so here's only an abstract method
   public abstract void getPackageConfigs(Configuration configuration);
private class LocationConfigurationProvider implements ConfigurationProvider {

       Configuration configuration;
       public void destroy() {
           //waitInterceptor.destroy();
       }

       public boolean needsReload() {
           return false;
       }

public void init(Configuration configuration) throws ConfigurationException {
           this.configuration = configuration;
       }

       public void loadPackages() throws ConfigurationException {
              getPackageConfigs(configuration);
       }

public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
           builder.factory(ObjectFactory.class);
builder.factory(ActionProxyFactory.class, DefaultActionProxyFactory.class);
       }
   }
}

The problem I've found is that it is not possible to execute
super.setUp()

When adding or uncommenting this line, I get an error

17 Jul 2007 18:42:01,589 - FATAL org.apache.struts2.spring.StrutsSpringObjectFactory - ********** FATAL ERROR STARTING UP SPRING-STRUTS INTEGRATION **********
Looks like the Spring listener was not configured for your web app!
Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
You might need to add the following to web.xml:
   <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

The Spring listener IS configured in my web.xml, but it seems that the test case cannot find the web.xml file itself, even though I put the WEB-INF directory, and the directory containing the WEB-INF directory in the classpath. I also have the impression that my current Test configuration does not use the struts.xml file at all (that's why I have to explicitly set the package configuration via the getPackageConfigs method). This is a pity, since I would like to have any modification in my struts.xml file reflected in my tests.

Is it possible anyhow to configure the StrutsTestCase to use the web.xml file. Is there any documentation on how to configure the StrutsTestCase correctly to use the web configuration ?

Any pointers are welcome.

Thanks,

bartlebooth


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to