【问题标题】:How to use Mockito.doAnswer() with a void method with no parameters?如何将 Mockito.doAnswer() 与没有参数的 void 方法一起使用?
【发布时间】:2020-09-09 18:14:29
【问题描述】:
@Test
public void successfulHandshake(){
    HandshakeImpl1Server handShake = new HandshakeImpl1Server();
    handShake.setHttpStatus((short) 101);

    authUnderTest.authenticate(callback);

    doAnswer(invocation -> {
        websocket.onOpen(handShake);
        return null;
    }).when(websocket).open();
    
    verify(websocket,times(1)).send(any(String.class));
}

doAnswer 永远不会被调用。有什么想法吗?

【问题讨论】:

标签: java android testing mockito


【解决方案1】:

您需要在调用被测方法之前而不是之后存根方法。

在调用 authUnderTest.authenticate 之前移动 doAnswer

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 2021-02-05
    • 1970-01-01
    • 2013-02-02
    相关资源
    最近更新 更多