尝试使用Mockito模拟IntConsumer失败。

huangapple go评论133阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年5月30日 19:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/62102080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定