[英]NullPointerException with mockito 2
I get java.lang.NullPointerException
with this following code:我使用以下代码得到java.lang.NullPointerException
:
@Mock
SomeClass retObj;
when(obj.getXXXX(anyString())).thenReturn(retObj);
// obj.getXXXX(null) shows null in other methods
It used to work fine in Mockito 1.它曾经在 Mockito 1 中运行良好。
I have fixed it.我已经修好了。 It turns out Mockito 2.0 handles null
differently.事实证明 Mockito 2.0 处理null
不同。 When you setup mock as当您将模拟设置为
when(obj.getXXXX(anyString())).thenReturn(retObj);
But actually the call is obj.getXXXX(null)
it does not return retObj
.但实际上调用是obj.getXXXX(null)
它不返回retObj
。 Instead it returns null
.相反,它返回null
。 Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.