0

这些天我一直在使用 jmock 和 seam,但它不足以模拟最终/静态/枚举。所以我尝试使用 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();
}

}

如果我在类声明(公共类消费者..)处设置断点,则跳到下一行会导致 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

一般反对意见:模拟是为了将您与外部代码隔离开来,尤其是与来自您的服务器 wendor 的那些代码(如 AbstractSeamtest ) - 所以您不必初始化它们或让服务器运行或其他任何东西。

将模拟测试视为已保存的调试会话。在您的情况下,您想保证(我猜),该方法

bean.saveSectionChanges();

与接缝基础设施或其他合作者正确交互。这很容易通过以下方式实现:

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 回答