0

最近ずっと jmock を seam で使用していましたが、final/static/enum をモックするには不十分です。そこで、JMockit を使ってみました。ただし、実行するたびに NPE が発生します。デバッグすらできません。以下はサンプルコードです

public class TestJmockit extends SeamTest {

@Mocked Dependency dependencyInCodeToTest;
CodeToTest bean = new CodeToTest();

@Test
 public void testSaveSectionChangesJMockit() throws Exception {
    new AbstractSeamTest.ComponentTest() {
        @Override
        protected void testComponents() throws Exception {

                new NonStrictExpectations()
                {
                  {
                    dependencyInCodeToTest.getLabel(); result = "Normal";
                  }
                };

                bean.execute();
        }
    }.run();
}

}

実際のコード..

package com.abc.action.account.information;

import com.abc.vo.account.ExternalAccountStatus;
import com.abc.vo.account.information.ExternalAccountStatusClosedInfo;
import com.abc.vo.account.information.ExternalAccountStatusInfo;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import org.jboss.seam.mock.AbstractSeamTest;
import org.jboss.seam.mock.SeamTest;
import org.junit.Test;


public class ConsumerAccountInformationActionTestJmockit extends SeamTest {

    @Mocked ExternalAccountStatus mockExternalAccountStatus;
    @Mocked ExternalAccountStatusInfo mockExternalAccountStatusInfo;

//    ConsumerAccountInformationAction bean = new ConsumerAccountInformationAction();


@Test
 public void testSaveSectionChangesJMockit() throws Exception {
    new AbstractSeamTest.ComponentTest() {
        @Override
        protected void testComponents() throws Exception {

                new NonStrictExpectations()
                {
                  {
                    mockExternalAccountStatus.getLabel(); result = "Normal";
                    mockExternalAccountStatusInfo.getClosedInfo(); result = new ExternalAccountStatusClosedInfo();
                  }
                };

//                bean.saveSectionChanges();
        }
    }.run();
}

}

クラス宣言 (Public Class Consumer..) にブレークポイントを設定すると、次の行にステップ オーバーすると NPE が発生します。コード内のコメント行を削除すると、コメント解除された最初の行で失敗します。

Java 1.6 と IntelliJ IDE を使用しています。IDE構成と関係があるのだろうか。

TestNG ではスタック トレースすら取得できませんが、JUnit では以下のように表示されます。

java.lang.NullPointerException
    at org.jboss.seam.servlet.ServletApplicationMap.get(ServletApplicationMap.java:54)
    at org.jboss.seam.contexts.BasicContext.get(BasicContext.java:49)
    at org.jboss.seam.contexts.BasicContext.get(BasicContext.java:44)
    at org.jboss.seam.core.Init.instance(Init.java:117)
    at org.jboss.seam.contexts.BusinessProcessContext.<init>(BusinessProcessContext.java:47)
    at org.jboss.seam.contexts.TestLifecycle.beginTest(TestLifecycle.java:35)
    at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:159)
    at com.billmelater.csa.action.account.information.ConsumerAccountInformationActionTestJmockit.testSaveSectionChangesJMockit(ConsumerAccountInformationActionTestJmockit.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


Process finished with exit code 255
4

1 に答える 1

0

一般的な反論: モッキングは、外部コード、特にサーバー ベンダー ( AbstractSeamtest など) からのコードからユーザーを分離するためにあります。

模擬テストを保存されたデバッグ セッションとして扱います。あなたの場合、あなたが保証したい(私は推測しています)、その方法

bean.saveSectionChanges();

Seam インフラストラクチャまたは他の協力者と正しく対話します。これは、次のような方法で簡単に実現できます。

public static testProperInteraction(@Mocked final Collaborator collaborator) {
    new Expectations() {
        {
            collaborator.doThis(with some parameters);
            returns(something you like);
        }
    };

   Bean bean = new Bean(collaborator);

  assertSomething(bean.saveSessionChanges());

   // nothing else shall be called
    new FullVerifications() {
        {
        }
    };
} 
于 2011-12-20T11:26:32.830 に答える