0

Struts2.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。これは些細な例なので、基本的なものが欠けているに違いありません。残念ながら、私は春をまったく知りません。

これが(削除された)アクションクラスです:

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例外が発生するのはなぜですか?また、それに対して何ができますか?

4

1 に答える 1

0

ソリューション:

モックスタックは、モックしているアクションの完全修飾クラス名を期待します。そうでない場合は、ActionSupportを想定します。この行:

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

StrutsでのJunitテストのチュートリアルやイントロでこれについて言及しているものはありません。実際、あなたがすでにSpringマスターでない限り、それらのほとんどはコンパイルするのが面倒です。

于 2013-03-14T19:00:29.963 に答える