【问题标题】:Getting "org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers" inconsistently不一致地获取“org.mockito.exceptions.misusing.InvalidUseOfMatchersException:参数匹配器的无效使用”
【发布时间】:2020-12-22 22:27:05
【问题描述】:

一位开发人员编写了一个使用 mockito verify 的测试代码

verify(spyService, times(1)).register("ios", any());

有趣的是,这段代码在他们的本地机器和 jenkins 构建环境中执行得很好。代码投入生产。但是代码在我的本地失败了

Caused by: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
4 matchers expected, 1 recorded:
-> at com.tile.services.rest.service.insurance.InsuranceCoverageServiceTest.testNonPartnerTilesWithoutTosAcceptance(InsuranceCoverageServiceTest.java:163)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

鉴于该项目通过 gradle 结构良好,junit 版本在任何地方都是相同的。 您知道导致这种不一致的原因吗?

【问题讨论】:

  • 您是否在本地运行与其他开发人员完全相同的 Java 版本? IDE 没有覆盖任何东西?

标签: java-8 mockito junit4


【解决方案1】:

你已经用过了吗?

verify(spyService, times(1)).register(eq("ios"), any());

在您的行中,您使用的是特定值 "ios" 和匹配器 any(),我们不能同时使用两者。 保持坚强

【讨论】:

    猜你喜欢
    • 2022-07-20
    • 2020-09-17
    • 2020-03-23
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多