2013-03-14 4 views
0

Struts 2.3.8과 함께 제공되는 스프링 모의 프레임 워크를 사용하는 유닛 테스트를 작성하는 데 문제가 있습니다. 기본적으로 프레임 워크를 빌드하는 BaseTestCase가 있습니다. 그런 다음 개별 테스트 케이스를 호출합니다. 내가 얻을 조롱 행동과 createAction 나오는 : 호기심CastClassException Struts2 액션을위한 테스트 케이스 구현

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 때문이다. 이것이 사소한 예이기 때문에 나는 기본적인 것을 놓치고 있어야합니다. 불행하게도 나는 봄을 전혀 모른다.

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'의 부하와 싸우고있는 것처럼 보입니다. –

+0

나는 약간의 테스트를 수행했으며, 동일한 로더로 간주되어 proxy.getClass(). getClassLoader()를 확인했다. 내가 볼 필요가있는 다른 것이 없으면? – WPrecht

+0

당신은 당신이 가지고있는 것이 무엇이든지간에'toString' 시도 했습니까? 그 빛이 비춰 지는지 확인하기 위해서? –

답변

0

솔루션 :

모의 스택이 조치의 완전한 클래스 이름은 당신이 조롱하거나 다른 사람이 ActionSupport를 가정 기대하고있다. 이 줄은 :

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

Struts에 대한 Junit 테스트의 튜토리얼이나 소개에는이 내용이 언급되어 있지 않습니다. 사실, 당신이 이미 Spring master가 아니라면, 대부분 컴파일하기가 어렵습니다.