Skip to content
Advertisement

Trying to mock IntConsumer with Mockito fails

I’m trying to mock IntConsumer:

class TickerServiceImplTest {
@Test
void testRunIterations() {
    TickerServiceImpl tickerService = new TickerServiceImpl();
    int ticksToRun = 100;
    tickerService.setTicksToRun(ticksToRun);
    IntConsumer intConsumerMock = mock(IntConsumer.class);
    tickerService.run(intConsumerMock);
    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()

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement