I think I did something terrible to my last message. Please ignore any
duplication.

In short Cactus and Struts get along just great. If you search the mail
archives for this list, you'll find several messages on the topic so far,
with valuable suggestions and sample code. Basically there are two
alternatives.

1. Unit style
2. Integration Style

I use Unit style, which relies on mocked implementations of some of Struts'
classes to isolate the behavior of my actions. Here are some methods from a
test of my Login action:

    /** Test of perform method, of class
storeblox.login.actions.LoginAction. */
    public void testPerform() throws Exception {

        form.setName("badName");
        form.setPassword("somePassword");

        assertPerform("input");

        ActionErrors errs =
(ActionErrors)request.getAttribute(action.ERROR_KEY);
        assertEquals(1, errs.size());
        assertEquals(1, errs.size("login"));
    }

    public void testGoodLogin()throws Exception{
        form.setName("goodName");
        form.setPassword("goodPass");

        assertPerform("confirmation");
    }

assertPerform is a dubiously named method which calls perform and asserts
that the login forwards to the correct location.

    public void assertPerform(String expectedForwardPath) throws Exception{
        ActionForward fwd = action.perform(mapping, form, request,
response);
        assertEquals(expectedForwardPath, fwd.getPath());
    }

Since action forms are simple value objects I use a real one. The mapping is
a mock object that extends ActionMapping. Here are my instance variables and
setup method:

    private LoginAction action;
    private FakeMapping mapping;
    private LoginForm form;

    public void setUp(){
        action = new LoginActionMock();
        mapping = new FakeMapping();
        mapping.addForward("confirmation", "confirmation");

        form = new LoginForm();
    }

Here is the mock action mapping:

package eblox.commons.testutils;

import org.apache.struts.action.*;
import java.util.*;

public class FakeMapping extends ActionMapping{

    public FakeMapping(){
        addForward("success", "successForward");
        addForward("failure", "failureForward");
    }

    public ActionForward findForward(String s){
        return new ActionForward((String)map.get(s));
    }

    public void addForward(String name, String forward){
        map.put(name, forward);
    }

    public String getInput(){
        return "input";
    }

    private Map map = new HashMap();
}

I forget the details of the Integration style tests. Basically instead of
using a fake mapping, an integration test would retrieve one from the Struts
action servlet.

Anyway, let me know if this has helped. Given the volume of this topic, is
it worth creating a tutorial specifically on integrating the two products?

Cheers,

nick

-----Original Message-----
From: shridhar MD [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, October 23, 2001 11:48 PM
To: [EMAIL PROTECTED]
Subject: Cactus : Struts


Hi Cactus folks,

I want to know whether Cactus ( latest version ) can be used for Testing
applications developed on Struts Framework.

Thanks.

Reply via email to