我正在尝试使用Mockito创建从Mock对象返回的Mock对象。具体来说,我正在尝试模拟一个PlayerConnection
程序可以用来检索IP地址的对象。
您可以PlayerConnection object
在这里找到更多有关此的信息。它返回一个InetSocketAddress
,然后InetAddress
可以返回一个可以返回String
播放器IP的。但是我不能走那么远,因为我的第一个when(class.function()).thenReturn(returnVariable)
抛出NullPointerException
。这是我的代码:
/**
* Creates a partial mock of a connection that can return an ip address.
*
* @param String
* The IP to return when the connection gets asked.
* @return
*/
private PlayerConnection newConnection(String ipString)
{
PlayerConnection playerConnection = mock(PlayerConnection.class);
InetSocketAddress inetSocketAddress = mock(InetSocketAddress.class);
InetAddress inetAddress = mock(InetAddress.class);
when(playerConnection.getAddress()).thenReturn(inetSocketAddress);
when(inetSocketAddress.getAddress()).thenReturn(inetAddress);
when(inetAddress.getHostAddress()).thenReturn(ipString);
return playerConnection;
}
这是堆栈跟踪,发生在when(playerConnection.getAddress()).thenReturn(inetSocketAddress)
:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.001 sec <<< FAILURE!
ruleResponseTest(com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest) Time elapsed: 0.001 sec <<< ERROR!
java.lang.NullPointerException
at java.net.InetSocketAddress$InetSocketAddressHolder.access$500(InetSocketAddress.java:56)
at java.net.InetSocketAddress.getAddress(InetSocketAddress.java:334)
at com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest.newConnection(RuleManagerTest.java:99)
at com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest.ruleResponseTest(RuleManagerTest.java:44)
编辑:
我已将桩号切换为doReturn().when().function()
而不是when().thenReturn()
停止NullPointerExceptions
,但确实如此,但是现在我UnfinishedStubbingExceptions
从Mockito获得了自定义。
有用的错误代码说我的某个地方存根未完成,但看不到它在哪里。该错误发生在第二种doReturn()
方法上。
/**
* Creates a partial mock of a connection that can return an ip address.
*
* @param ipString The IP to return.
*
* @return A PlayerConnection object that can return a Host Address of the ipString but nothing else.
*/
private PlayerConnection newConnection(String ipString)
{
PlayerConnection playerConnection = mock(PlayerConnection.class);
InetSocketAddress inetSocketAddress = mock(InetSocketAddress.class);
InetAddress inetAddress = mock(InetAddress.class);
doReturn(inetSocketAddress).when(playerConnection).getAddress();
doReturn(inetAddress).when(inetSocketAddress).getAddress();
doReturn(ipString).when(inetAddress).getHostAddress();
return playerConnection;
}
摘要: InetSocketAddress.getAddress()
最终确定,如docs中所列。由于其巧妙的语法,Mockito无法轻易地存根或验证final
方法,甚至在尝试失败时也无法告诉您。通常,不要嘲笑您无法控制的对象,尤其是由于这种情况。
UnfinishedStubbingException的有用错误代码可识别您的问题(请参阅选项#2,您这调皮的开发人员!):
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
详细信息:常规Mockito模拟通过获取一个对象并自动且无提示地生成一个子类来工作,在该子类中,所有方法都被重写以委托给Mockito。但是,Java编译器将静态分派用于最终方法,因此对最终方法的调用不会直接转到Mockito,而是转到InetSocketAddress中的实际方法实现。该子类实例从未打算与之交互,并且未设置任何字段,因此很容易获得NullPointerException或其他行为-所有这些都发生在与Mockito交互之前,因此您无需甚至在使用时也可以从错误消息中受益when(object.methodCall()).thenReturn()
,因为object.methodCall()
在Mockito完全参与评估之前,意外的NPE发生了。
用另一种方式doReturn(...).when(object).methodCall()
做事-使Mockito有机会看到doReturn
,when
即使对的调用methodCall
没有委托给Mockito。这使Mockito有上下文可以说存根未完成,因为Mockito无法看到methodCall
呼叫。
有关更多信息,请参见此SO问题(Final方法模拟),并考虑使用Mockito在2.1版中引入的final类和方法的最新选择加入模拟。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句