Trying to mock IntConsumer with Mockito fails

Tags: , ,

I’m trying to mock IntConsumer:

class TickerServiceImplTest {
void testRunIterations() {
    TickerServiceImpl tickerService = new TickerServiceImpl();
    int ticksToRun = 100;
    IntConsumer intConsumerMock = mock(IntConsumer.class);;
    verify(intConsumerMock, times(ticksToRun));

and it fails on the ‘verify’ with below error code:

Method threw 'org.mockito.exceptions.base.MockitoException' exception. Cannot evaluate $java.util.function.IntConsumer$$EnhancerByMockitoWithCGLIB$$3ee084c4.toString()


You need to tell Mockito what method it is supposed to verify on the IntConsumer mock. Your verification code should look something like:

verify(intConsumerMock, times(ticksToRun)).accept(anyInt());

See for example the tutorial at Baeldung.

Source: stackoverflow