我在构建使用 Struts 2.3.8 附带的 Spring 模拟框架的单元测试时遇到了麻烦。基本上,我有一个构建框架的 BaseTestCase。然后是调用它的单个测试用例。用我得到的模拟动作从 createAction 中出来:
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)
这是因为动作类很好奇extends ActionSupport
。我必须缺少一些基本的东西,因为这是一个微不足道的例子。不幸的是,我根本不了解Spring。
这是(精简的)动作类:
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;
}
}
和测试用例(在第一行爆炸):
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");
}
以及模拟堆栈的baseCase:
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();
}
为什么我会收到 ClassCast 异常,我该怎么办?