gpt4 book ai didi

Mockito NullPointerException 使用任何

转载 作者:行者123 更新时间:2023-12-04 17:45:24 24 4
gpt4 key购买 nike

我正在尝试像这样使用 Mockito:

    Mockito.when(Mockito.any(ObjectMapper.class).readValue(Mockito.any(BufferedReader.class),Mockito.any(Class.class))).thenReturn(new Person("1","abc"));

这是来自 jackson 图书馆。
public <T> T readValue(Reader src, Class<T> valueType)

我这样做的原因是因为当我到达代码的这一点时,已经创建了大量对象。 mock 每一步都需要时间。

当代码到达这个 mockito 语句时我得到 NPE 的任何原因?

堆栈跟踪 :
java.lang.NullPointerException
at com.prashant.flax.ShellTest.givenDirectoryHasFiles(ShellTest.java:139)
at com.prashant.flax.ShellTest.testExecute(ShellTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

正如你所看到的,它在一个给定的方法中(这个方法只有这段代码),所以我可以在调试时看到它到达那里并崩溃。

最佳答案

正如 Oliver 在评论中提到的,您不能申请 when发生在所有对象上。 Mockito 通过子类化工作,因此您必须使用 mock 创建一个模拟实例, spy ,或 @Mock@Spy注解;自定义行为;然后安装模拟 using dependency injection or other similar tricks .

至于为什么会发生这种情况,返回值 any()实际是null ;匹配器喜欢 any只能用作 when 的参数和 verify ,并且 Mockito 无法生成 Class 的专门实例表示“任何类”,因此 Mockito 返回一个虚拟值(空)并将数据存储在专门的参数匹配器堆栈上。尽管 Mockito 有更好的错误消息来提醒您注意这种情况,但 Mockito 之前的代码 NPE 可以通过使用示例为您提供适当的异常。

有关匹配器返回值和堆栈的更多信息,请参阅我在 "How do Mockito matchers work?" 上的其他 SO 答案。 .

关于Mockito NullPointerException 使用任何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36272751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com