标题翻译
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.
专注分享java语言的经验与见解,让所有开发者获益!
评论