Mockito when()。thenReturn()抛出nullpointerExceptions

道伊

我正在尝试使用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有机会看到doReturnwhen即使对的调用methodCall没有委托给Mockito。这使Mockito有上下文可以说存根未完成,因为Mockito无法看到methodCall呼叫。

有关更多信息,请参见此SO问题(Final方法模拟),并考虑使用Mockito在2.1版中引入的final类和方法的最新选择加入模拟

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

基于输入参数Mockito.when thenReturn

的Mockito - 当thenReturn

如何检查异常未被使用抛出的Mockito?

Mockito thenThrow抛出模仿异常

Mockito的thenReturn中的Java枚举列表

Mockito-thenReturn总是返回null对象

Mockito when()。thenReturn()无法正常工作

Mockito中的动态thenReturn()语句

Mockito抛出UnfinishedStubbingException没有明显的原因

Mockito如何模拟和声明抛出的异常?

Mockito when()。thenReturn不必要地调用该方法

从Mockito抛出异常

Mockito when and thenReturn语句不适用于Java中的2个方法调用

Mockito thenReturn返回相同的实例

Mockito:如何动态设置thenReturn()?

Mockito when()。thenReturn()当返回空列表时返回Null

方法Mockito.when()。thenReturn()和Mockito.doreturn()。when()不返回指定的模拟值

Mockito.when()。thenReturn()不起作用或返回null

Mockito和PowerMock MethodNotFoundException被抛出

Mockito when ... thenReturn不返回Collection的期望值

Mockito when-thenReturn with SharedPreference值

Mockito when thenreturn返回null

Mockito when()... then()NullPointerException

Mockito NoSuchElementException when() findById() get() thenReturn()

如何使用 Mockito 抛出 SQLServerException(或 SQLException)?

mockito,当使用 when/thenReturn 设置模拟存根时,它出现异常

Mockito.when(X).thenReturn(Y) 是否有可能返回 Set<Optional<MyClass>>?

SpringBoot Mockito:when..thenReturn 給出異常

mockito when,thenreturn("Hello") 不正确地匹配正则表达式