(Sorry, hit the wrong key combination and hit send without meaning to...)

I am attempting to do some out-of-container testing of a Struts Action
which extends StrutsSpringTestCase. I've tried configuring the
application context two separate ways: via the @ContextConfiguration
annotation and by overriding getContextLocations(), with no luck.

The project is set up on a "standard" Maven2 layout, so the
/src/test/resources directory has its own copy of
applicationContext.xml. However, on attempting to run the unit test, I
get a NullPointerException when my code tries to call
getActionProxy(). Here's the Exception text:

java.lang.NullPointerException
        at 
org.apache.struts2.StrutsTestCase.getActionProxy(StrutsTestCase.java:130)
        at 
org.jason.application.web.actions.struts2.TestLoginAction.testRegister(TestLoginAction.java:72)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at 
org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:160)

Here's the code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
public class TestLoginAction extends StrutsSpringTestCase {

    private AccountService accountService;  // Mock AccountService
object, we'll need it for testing register()

    @Before
    public void setUp() {

        accountService = EasyMock.createMock(AccountService.class);

    }

    @Override
    protected String getContextLocations() {
        return "classpath*:applicationContext.xml";
    }

    @Test
    public void testRegister() throws Exception {

        // setup the account we expect to be a duplicate
        Account duplicateAccount = new Account();
        duplicateAccount.setFirstName("Duplicate");
        duplicateAccount.setLastName("Account");
        duplicateAccount.setOrganization("None");

        // setup the account we expect to be good
        Account goodAccount = new Account();
        duplicateAccount.setFirstName("John");
        duplicateAccount.setLastName("Smith");
        duplicateAccount.setOrganization("None");

        Account goodAccountPopulated = goodAccount;
        goodAccount.setId(501);

        // setup the mock object
        EasyMock.expect(accountService.createAccount(duplicateAccount))
                .andThrow(new DuplicateUsernameException("Account name
already exists"));
        
EasyMock.expect(accountService.createAccount(goodAccount)).andReturn(goodAccountPopulated).anyTimes();

        EasyMock.replay();

        // get the action
        ActionProxy proxy = getActionProxy("/login/register");
        LoginAction action = (LoginAction) proxy.getAction();

        // inject the mock into the action
       action.setAccountService(accountService);

        // test for validation errors
        request.setParameter("model.lastName", "MyLastName");
        request.setParameter("model.organization", "None");
        String result0 = proxy.execute();
        assertTrue("Problem: result of registration attempt without
required fields should have returned INPUT",
                result0.equals("input"));
        assertTrue("Problem: field model.firstName not included in
fieldErrors but should have been",
                action.getFieldErrors().containsKey("model.firstName"));

        // test for duplicate registration
        request.setParameter("model.firstName","Duplicate");
        request.setParameter("model.lastName", "Username");
        request.setParameter("model.organization", "None");

        String result1 = proxy.execute();
        assertTrue("Problem: result of duplicate registration attempt
should have returned INPUT"
                    ,result1.equals("input"));  // ensure the error
result was returned
        assertTrue("Problem: no errors were present in fieldErrors but
should have been", action.getFieldErrors().size() > 0);

    }
}

Any ideas out there?

Jason

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to