Announcement Announcement Module
Collapse
No announcement yet.
Unit testing Struts actions out of the container? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unit testing Struts actions out of the container?

    I am using MockRunner, and extending ActionSupport to retrieve the WebApplicationContext().

    Is it possible to unit test struts actions out of the container? Any sample code would be greatly appreciated.

  • #2
    Re: Unit testing Struts actions out of the container?

    Originally posted by eglim
    I am using MockRunner, and extending ActionSupport to retrieve the WebApplicationContext().

    Is it possible to unit test struts actions out of the container? Any sample code would be greatly appreciated.
    Take look at Matt's Raible Spring Life sample chapter (free) see StrutsTestCase (http://strutstestcase.sourceforge.net/).

    Artur

    Comment


    • #3
      Thanks for the pointer. However, I'm not using a plain Struts action. I'm using getWebApplicationContext() from ActionSupport, and this method fails when I use unit test, stating that the ContextLoader plugin listener is not loaded.

      As you can see, my Struts action is integrated with Spring support, so I am figuring out how to unit test from that point of view.

      I haven't got StrutsTestCase working yet, but it could solve my problem if / when I get it working.

      In the meantime, I'm attempting to get it working with the mockrunner approach by creating my own mock WebApplicationContext programmatically and storing it in the mock servlet context.
      Code:
      /* TODO: code to create mock WebApplicationContext */
      
      ServletContext servletContext = getWebMockObjectFactory().getMockActionServlet().getServletContext();
      servletContext.setAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX, mockWebApplicationContext);

      Comment


      • #4
        Originally posted by eglim
        Thanks for the pointer. However, I'm not using a plain Struts action. I'm using getWebApplicationContext() from ActionSupport, and this method fails when I use unit test, stating that the ContextLoader plugin listener is not loaded.
        I strongly suggest You look into Matt's book. You can look into org.springframework.test package and matt's app fuse.

        regards,
        Artur

        Comment


        • #5
          The org.springframework.test package looks extremely helpful. I'll post the results if I have time to investigate later, and Matt Raible's AppFuse as well.

          in the mean time, I have come up with mockrunner support to read an existing applicationContext.xml as if it was actually deployed in a war. I hope this will be of use to someone.

          EDIT: I rewrote it into a MockRunnerHelper class. Just set up your WebMockObjectFactory.getMockServletContext() with any resources, then
          call the static method loadSpringContext() with your context file paths.

          Code:
          package mypackage;
          
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.IOException;
          
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
          import org.springframework.web.context.ConfigurableWebApplicationContext;
          import org.springframework.web.context.support.XmlWebApplicationContext;
          import org.springframework.web.struts.ActionServletAwareProcessor;
          import org.springframework.web.struts.ContextLoaderPlugIn;
          
          import com.mockrunner.mock.web.MockActionServlet;
          import com.mockrunner.mock.web.WebMockObjectFactory;
          
          /**
           * This class provides convenience methods to configure the MockRunner.
           */
          public class MockRunnerHelper {
          	/**
          	 * Sets the resource as stream to the MockServletContext of
          	 * this WebMockObjectFactory.
          	 * 
          	 * @param webMockObjectFactory
          	 * @param resourceName
          	 * @param filePath
          	 * @throws IOException
          	 */
          	public static void setResourceAsStream(WebMockObjectFactory webMockObjectFactory, String resourceName, String filePath)
          	throws IOException {
          		File file = new File(filePath);
          		FileInputStream streamer = new FileInputStream(file);
          	    byte[] byteArray=new byte[streamer.available()];
          		streamer.read(byteArray);
          
          		webMockObjectFactory.getMockServletContext().setResourceAsStream(resourceName, byteArray);		
          	}
          
          	public static ConfigurableWebApplicationContext loadSpringContext(WebMockObjectFactory webMockObjectFactory, String[] contextFilePaths) 
          	throws IOException {
          		StringBuffer resourceNameBuf = null;
          		String resourceName = null;
          		String[] configLocations = new String[contextFilePaths.length];
          		for &#40;int i=0; i<contextFilePaths.length; i++&#41; &#123;
          			resourceNameBuf = new StringBuffer&#40;"/WEB-INF/applicationContext"&#41;;
          			resourceNameBuf.append&#40;i&#41;;
          			resourceNameBuf.append&#40;".xml"&#41;;
          			resourceName = resourceNameBuf.toString&#40;&#41;;
          			
          			setResourceAsStream&#40;webMockObjectFactory, resourceName, contextFilePaths&#91;i&#93;&#41;;
          			configLocations&#91;i&#93; = resourceName;
          		&#125;
          		
          		final MockActionServlet actionServlet = webMockObjectFactory.getMockActionServlet&#40;&#41;;
          		ConfigurableWebApplicationContext wac = new XmlWebApplicationContext&#40;&#41;;
          		wac.setServletContext&#40;actionServlet.getServletContext&#40;&#41;&#41;;
          		wac.setConfigLocations&#40;configLocations&#41;;
          		wac.addBeanFactoryPostProcessor&#40;
          				new BeanFactoryPostProcessor&#40;&#41; &#123;
          					public void postProcessBeanFactory&#40;ConfigurableListableBeanFactory beanFactory&#41; &#123;
          						beanFactory.addBeanPostProcessor&#40;new ActionServletAwareProcessor&#40;actionServlet&#41;&#41;;
          					&#125;			
          		&#125;&#41;;
          		wac.refresh&#40;&#41;;
          
          		actionServlet.getServletContext&#40;&#41;.setAttribute&#40;ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX, wac&#41;;
          		return wac;
          	&#125;
          &#125;

          Comment

          Working...
          X