gpt4 book ai didi

java - Mockito Java doAnswer

转载 作者:行者123 更新时间:2023-12-01 21:28:18 26 4
gpt4 key购买 nike

这不会编译。我收到一个 UnfinishedStubbingException。我已经阅读了 Mockito api 和本网站上的其他问题,我认为我的语法应该没有错误,但它在 doAnswer(new Answer() {) 处失败,所以我认为它必须错了,但我不知道哪里错了。谢谢。

doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
((CrestronNioSocketHandler.NioEventReceiver) args[0]).onDataReceived(new byte[wantedNumber]);
return null;
}
}).when(mockedChannel.read(any(ByteBuffer.class)));

最佳答案

应该是这样的

doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
((CrestronNioSocketHandler.NioEventReceiver) args[0]).onDataReceived(new byte[wantedNumber]);
return null;
}
}).when(mockedChannel).read(any(ByteBuffer.class));

检查this question关于使用 Mockito 进行 stub 的不同方法。

关于java - Mockito Java doAnswer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37756403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com