Question

I am having trouble getting unit tests built that use the Spring mock framework that comes with Struts 2.3.8. Basically, I have a BaseTestCase that builds the framework. And then individual test cases that call into it. Coming out of the createAction with the mocked up action I get:

java.lang.ClassCastException: com.opensymphony.xwork2.ActionSupport incompatible with com.lm.learn.action.LoginAction
at com.lm.learn.action.LoginActionTest.testUsername(LoginActionTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Which is curious because the action class extends ActionSupport. I must be missing something basic as this is a trivial example. Unfortunately, I don't know Spring at all.

Here is the (stripped down) action class:

public class LoginAction extends ActionSupport {

private static final long serialVersionUID = -8123618443227375443L;
private String username;
private String password;
private User user;


public String execute() {

    user = new User(username, password);
    if (validateLogin(user)) {
        return SUCCESS;
    } else {
        addActionError(getText("error.login"));
        return ERROR;
    }
}

And the test case (blows up on the first line):

    public void testUsername() throws Exception {

    action = (LoginAction) createAction(LoginAction.class, "/", "LoginAction", "execute");
    Map<String, Object> p = new HashMap<String, Object>();
    p.put("user.userName", "admin");
    p.put("user.password", "admin");
    proxy.getInvocation().getInvocationContext().setParameters(p);
    String result = proxy.execute();
    assertEquals(result, "success");
}

And the baseCase that mocks the stack:

    protected <T> T createAction(Class<T> clazz, String namespace, String actionName, String methodName) throws Exception {

    proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class)
            .createActionProxy(namespace, actionName, methodName, null, false, false);
    proxy.getInvocation().getInvocationContext().setParameters(new HashMap<String, Object>());
    proxy.setExecuteResult(false);
    ServletActionContext.setContext(proxy.getInvocation().getInvocationContext());


    return (T) proxy.getAction();
}

Why am I getting a ClassCast exception and what can I do about it?

Was it helpful?

Solution

The solution:

The mock stack expects the FULLY QUALIFIED class name of the action you are mocking for or else it assumes ActionSupport. This line:

proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class)
        .createActionProxy(namespace, actionName, methodName, null, false, false);

Not one tutorial or intro to Junit testing on Struts mentions this. In fact, most of them are annoyingly hard to get compiling at all unless you are already a Spring master.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top