我正在尝试模拟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));
}


并且在“验证”中失败,并显示以下错误代码:

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

最佳答案

您需要告诉Mockito应该在IntConsumer模拟中验证哪种方法。您的验证码应类似于:

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


例如,请参见Baeldung上的教程。

07-25 21:27