Mockito Java doAnswer

时间:2016-06-10 20:08:08

标签: java mockito

这不会编译。我得到一个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)));

1 个答案:

答案 0 :(得分:2)

应该是这样的

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进行存根的不同方法。