2013-03-14 44 views
0

我無法獲得使用Struts 2.3.8附帶的Spring模擬框架構建的單元測試。基本上,我有一個構建框架的BaseTestCase。然後個別的測試用例可以調用它。走出來嘲笑的後續行動,我得到了createAction的:CastClassException構建Struts2 Action的測試用例

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) 

這是因爲好奇action類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異常,我能做些什麼呢?

+0

看起來你有不同的'ClassLoader'的負載正在作戰。 – 2013-03-14 17:31:00

+0

我做了一些測試,似乎是一樣的加載器,檢查proxy.getClass()。getClassLoader()。除非有什麼我需要看看? – WPrecht 2013-03-14 18:11:04

+0

你有沒有嘗試'toString''無論你有什麼?只是爲了看看是否有任何亮光? – 2013-03-14 18:14:51

回答

0

解決辦法:

的模擬棧預計行動的完全限定類名你嘲笑的,否則它假定了ActionSupport。此行:

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

在Struts上沒有任何教程或Junit測試介紹提到了這一點。事實上,除非你已經是Spring大師,否則他們中的大多數都很難編譯。