UnitTest, Mock整数类型返回NPE(null)报错

Posted kingmax54212008

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UnitTest, Mock整数类型返回NPE(null)报错相关的知识,希望对你有一定的参考价值。

在Mock整数数据的时候,如果是使用any()则报如下错误。需要使用anyInt()。报错如下,具体分析参考下文:

java.lang.NullPointerException
	at xxx.WtfTest.testCaseNPE(WtfTest.java:34)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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.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.SilentJUnitRunner.run(SilentJUnitRunner.java:39)
	at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
	at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:103)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

具体分析:

带有的签名any()javac认为

  • any()可以返回Integer=>插入的期望是整数
  • Integer可以是装箱=>调用intValue

然而 any

  • 返回类型T的隐式上限为Object=> Integer匹配javac
  • 返回null引用=>确定Integer

NPE是通过未明确编写的代码在上引发的,我们在这里实际上不能做任何事情。注意any()  的javadoc 使用原始变量:

For primitive types use @link #anyChar() family...

以上是关于UnitTest, Mock整数类型返回NPE(null)报错的主要内容,如果未能解决你的问题,请参考以下文章

使用 unittest 中 mock 来模拟接口的返回

为啥我在使用 Mock 注释时有 NPE

使用 unittest.mock 在 python 中修补 SMTP 客户端

python unittest 之mock

[Python Modules] unittest.mock

使用 unittest.mock.patch 测试 aiohttp 客户端