i've had some success unit testing struts actions using cactus.
here's what i did:

0) replicate your approot in the test webapp, including struts-config.xml
1) in the web.xml (or webapp file for websphere test environment), add
struts initialization parameters to the cactus ServletRedirector servlet
2) write a class that extends org.apache.commons.cactus.ServletTestCase.
a skeletal example is below.

i'm interested to hear feedback on this approach.  overkill, not enough?
as it stands, i have to create an ActionServlet and init() it with the config
object i get for free from cactus (which because of step 1 has the config
params that struts needs).  seems kind of wacky.

public class SomeActionTest
extends ServletTestCase
{
  private ActionServlet servlet;
  private Action action;

  public SomeActionTest(String name)
  {
    super(name);
  }

  public static Test suite()
  {
    return new TestSuite(SomeActionTest.class);
  }

  public static void main(String[] args)
  {
    new junit.swingui.TestRunner().run(SomeActionTest.class);
  }

  protected void setUp() throws Exception
  {
    super.setUp();

    this.servlet = new ActionServlet();
    servlet.init(config);

    this.action = new SomeAction();
    action.setServlet(servlet);
  }

  protected void tearDown() throws Exception
  {
    servlet.destroy();

    super.tearDown();
  }

  public void testPerform()
  {
    try
    {
      ActionForward forward =
        action.perform(
          servlet.findMapping("/produceAForm"),
          null,
          request,
          response);

      assertEquals("success", forward.getName());

      ActionFormBean formBeanDef = servlet.findFormBean("aForm");
      Object obj = request.getAttribute("aForm");
      assertNotNull(obj);
      assertEquals(formBeanDef.getType(), obj.getClass().getName());
    }
    catch (Exception e)
    {
      fail("threw: " + e);
    }
  }

}


>>> [EMAIL PROTECTED] 07/31/01 04:04AM >>>

Hi John,
If you don't get any answer on the struts-user mailing list please repost on the 
cactus-user list, I know for sure that several persons have done it and would be able 
to give you the code. Basically, there is no helper class for the time being, meaning 
that before calling your action, you'll need to create needed Struts objects. But as 
Struts is very well designed and is using the "composition" approach (meaning that 
objects are not hard-linked with each other but rather you initialize an object by 
passing it needed domain objects) there is no problem with that and it works fine.

-Vincent
----- Original Message ----- 
From:John Yu
To:[EMAIL PROTECTED] 
Sent: Tuesday, July 31, 2001 8:31 AM
Subject: Unit testing Actions


Is there any sample showing how to do unit testing for Actions? How do I use Cactus to 
do it? 

Thanks.

-- 
John Yu Scioworks Technologies 
e: [EMAIL PROTECTED] w: +(65) 873 5989
w: <A href="http://www.scioworks.com&nbsp;http://www.scioworks.com<A 
href="http://www.scioworks.com&nbsp;m: +(65) 9782 9610 

Reply via email to