英文:
Trying to mock IntConsumer with Mockito fails
问题
我正在尝试模拟 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));
}
}
但在 'verify' 部分失败,出现以下错误代码:
Method threw 'org.mockito.exceptions.base.MockitoException' exception. Cannot evaluate $java.util.function.IntConsumer$$EnhancerByMockitoWithCGLIB$$3ee084c4.toString()
英文:
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()
答案1
得分: 0
你需要告诉Mockito应该在IntConsumer
模拟上验证哪个方法。你的验证代码应该类似于:
verify(intConsumerMock, times(ticksToRun)).accept(anyInt());
例如,可以参考Baeldung上的教程。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论