英文:
How to use Mockito.doAnswer() with a void method with no parameters?
问题
@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 永远没有被调用。有什么想法吗?
英文:
@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 is never called. Any ideas?
答案1
得分: 1
你需要在调用被测试方法之前进行方法存根,而不是之后。
在调用 authUnderTest.authenticate 之前移动 doAnswer。
英文:
You need to stub your methods before, not after call to method under test.
Move doAnswer before call to authUnderTest.authenticate
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论