Josh Suttenberg Josh Suttenberg - 5 months ago 26
Java Question

Mockito Java doAnswer

This won't compile. I get an

UnfinishedStubbingException
. I've read the Mockito api and other questions on this site and I think that my syntax shouldn't be wrong, but it fails at
doAnswer(new Answer() {
, so I figure it has to be wrong, but I don't know where. Thanks.

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)));

Answer

It should be like this

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));

Check this question about different ways of stubbing with Mockito.