【问题标题】:Method threw 'org.mockito.exceptions.misusing.InvalidUseOfMatchersException' exception.方法抛出 'org.mockito.exceptions.misusing.InvalidUseOfMatchersException' 异常。
【发布时间】:2018-07-31 20:00:07
【问题描述】:

我正在尝试测试此方法,但出现以下错误:

Method threw 'org.mockito.exceptions.misusing.InvalidUseOfMatchersException' exception. 

在这一行 代码:

    when(tester.method(
            any(String.class), any(LocalDate.class), any(boolean.class),any(boolean.class), any(String.class))).thenReturn(item);

【问题讨论】:

  • 你能发布更多你的测试代码吗?特别是围绕测试人员的嘲笑

标签: unit-testing testing junit


【解决方案1】:

对于您正在使用的大多数类型,已经有内置的匹配器。

when(tester.method(anyString(), any(LocalDate.class), anyBoolean(),anyBoolean(), anyString()))
.thenReturn(item);

参考Class ArgumentMatchers

对于原始类型,请使用any{Type}() family。

【讨论】:

  • 自 Mockito 2.1.0 起,anyChar() 仅适用于字符基元
  • @dementis 我直接从文档中引用。我能理解这种困惑。已更新。
【解决方案2】:

我会说你不应该使用boolean.class,因为小写布尔值是一个原始值,并且不返回 java.lang 对象。 使用Boolean.class,或anyBoolean()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多