尝试模拟在方法内创建的对象时,Mockito.spy出现NullPointerException

bfrts1fy  于 2022-11-08  发布在  其他
关注(0)|答案(2)|浏览(404)

你好,我正在尝试为我的apply函数写一个测试。我想做的是验证在执行函数时,try catch作用域中的方法是否被调用。下面是我的apply方法:

public Target apply(Source source) throws MappingException {
    try {
      Target target = targetModelObjectFactory.create(Target.class);
      mapNameToFirstName(source, target);
      mapMailToEMail(source, target);
      mapSourceSubEntityToTargetSubEntity(source, target);
      mapPrimitiveSourceColToPrimitiveTargetCol(source, target);
      mapSubEntitiesSourceColToSubEntitiesTargetCol(source, target);
      mapSourceSubEntityFieldToSubEntityFetchedField(source, target);
      produceProducedFieldValue(target);
      setConstantFieldConstantValue(target);
      return target;
    } catch (Exception e) {
      throw new MappingException(source, e);
    }
  }

这是我对mapMailToEMail(source, target);的测试

@Test

    public void TestSettingMappedMailFromSourceToTargetEMail() {

        Source mockedSource = Mockito.mock(Source.class);
        Target mockedTarget = Mockito.mock(Target.class);

        sourceToTargetMapper.mapMailToEMail(mockedSource, mockedTarget);
        Mockito.verify(mockedTarget, Mockito.times(1)).setEMail(mockedSource.getMail());
    }

这是我测试的一部分:

@Test
    public void TestApply() throws MappingException, MappingOperatorCreationException, TargetModelObjectCreationException {

        Source mockedSource = Mockito.mock(Source.class);
        TargetModelObjectFactory targetModelObjectFactory = Mockito.mock(TargetModelObjectFactory.class)
        Target mockedTarget = Mockito.spy(targetModelObjectFactory.create(Target.class));
        sourceToTargetMapper.mapNameToFirstName(mockedSource,mockedTarget);
        Mockito.verify(mockedSource,Mockito.times(1)).setMail(mockedSource.getMail());
}

我遇到的问题是

Target target = targetModelObjectFactory.create(Target.class);

在我的测试中,我需要模拟在方法中创建的Target对象,我尝试使用mockito.spy来实现这一点。但我一直得到NullPointerException。
这是正确的方法吗?我错过了什么?任何帮助都将不胜感激

编辑堆栈跟踪

java.lang.NullPointerException
    at org.mockito.Mockito.spy(Mockito.java:2034)
    at app.transcriber.example.generated.SourceToTargetMapperTest.TestApply(SourceToTargetMapperTest.java:87)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:54)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:99)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:105)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:40)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at com.sun.proxy.$Proxy5.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:119)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.base/java.lang.Thread.run(Thread.java:832)

app.transcriber.example.generated.SourceToTargetMapperTest > TestApply FAILED
    java.lang.NullPointerException at SourceToTargetMapperTest.java:87
1 test completed, 1 failed
yqkkidmi

yqkkidmi1#

你有一个简单的Map器,为什么要用mock来测试它呢?只要用一些输入调用它,然后检查输出就可以了。
如果你坚持:这个例外是因为你正在试图窥探一个通过调用targetModelObjectFactory.create(Target.class)创建的对象,而targetModelObjectFactory已经是一个模拟对象--你不能这么做,在一个模拟对象上调用的create返回null。要么创建一个真正的Target,要么创建一个真正的Factory

vdgimpew

vdgimpew2#

如果你使用Mockito.RETURNS_DEEP_STUBS参数得到一个空指针异常,我们告诉Mockito进行一种深度模拟。2这使得模拟一个完整方法的结果成为可能。

<Class_Name> ref = Mockito.mock(Class_Name.class, Mockito.RETURNS_DEEP_STUBS);

相关问题