Results 1 to 3 of 3

Thread: Unit testing a Struts Action with Spring integration

  1. #1
    Join Date
    Aug 2004
    Posts
    7

    Default Unit testing a Struts Action with Spring integration

    Hi,
    Are there any resources to help me unit test a Struts action that uses Spring beans? I am using the MockStrutsTestCase to test the action but when I insert the code into the action which retrieves beans from the WebApplicationContext, the Unit test fails. Any help would be much appreciated.

    Thanks.

    -Ed

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

    Default

    I use the following class when testing my Struts Actions using Struts Testcase
    Code:
    public abstract class BaseDynamicSpringStrutsTest extends MockStrutsTestCase {
    
      protected StaticWebApplicationContext wac = null;
    
      /**
       * getter for WebApplicationContext.
       */
      protected StaticWebApplicationContext getWebApplicationContext() {
        return this.wac;
      }
    
      /**
       * finalize the initialization of the WebApplicationContext.
       */
      public void finalizeSetUp() {
        //bind the WebApplicationContext to the servletContext
        ServletContext sc = getActionServlet().getServletContext();
        wac.setServletContext(sc);
    
        wac.refresh();
        sc.setAttribute(StaticWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
      }
    
    	protected void setUp() throws Exception {
        super.setUp();
        setInitParameter("validating", "false");
        wac = new StaticWebApplicationContext();
      }
    
    	protected void tearDown() throws Exception {
    	  wac = null;
        super.tearDown();
      }
    }
    If designed the class to use dynamically created StaticWebApplicationContext :lol:. This way I can register new beans dynamically into the WebApplicationContext. You can easily refactor the class to use a static applicationContext.xml
    Omar Irbouh

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

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

    Default

    sample usecase:
    Code:
    public class PasswordActionTestSuite extends BaseDynamicSpringStrutsTest {
    
      protected void setUp() throws Exception {
        //call parent setUp
        super.setUp();
    
        //create sample data
        List users = DomainUtils.populateUsers();
        List groups = DomainUtils.populateGroups();
    
        //retreive Spring WebApplicationContext and register userManager within Spring WebApplicationContext
        MutablePropertyValues pvs = new MutablePropertyValues ();
        pvs.addPropertyValue("users", users);
        pvs.addPropertyValue("groups", groups);
    
        wac.registerSingleton("userManager", MockUserManager.class, pvs);
    
        //ask parent class to finalize the setup
        finalizeSetUp();
      }
    
      protected void tearDown() throws Exception {
        super.tearDown();
      }
    
      public void testEditForm() {
        //redirect to editPassword
        setRequestPathInfo("/password.do");
    
        //populate form
        addRequestParameter("action", "editForm");
        addRequestParameter("id", "1");
        addRequestParameter("page", "0");
    
        //perform the action
        actionPerform();
    
        //verify that the action forwards to "tiles.passwordForm"
        verifyTilesForward("editForm", "tiles.passwordForm");
    
        //verify exceptions
        verifyNoActionErrors();
      }
    }
    HTH
    Omar Irbouh

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

Similar Threads

  1. Replies: 5
    Last Post: Oct 30th, 2012, 01:01 PM
  2. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  3. Replies: 4
    Last Post: Aug 1st, 2005, 03:45 PM
  4. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM
  5. Testing troubles with Struts / Spring
    By anthonyb in forum Web
    Replies: 4
    Last Post: Feb 10th, 2005, 03:44 PM

Posting Permissions

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