Josh Suttenberg
Josh Suttenberg

Reputation: 99

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

Upvotes: 1

Views: 1967

Answers (1)

Sergii Bishyr
Sergii Bishyr

Reputation: 8641

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.

Upvotes: 4

Related Questions