将任何无参数函数匹配为 scala Mockito 中的参数



我正在尝试验证使用以下方法是否使用 Mockito 调用:

class Notifier {
  def forward(request: ServletRequest)(onFailure: => Unit) : Unit
}

这是模拟上的验证:

val notifier = mock[Notifier]
there was one(notifier).forward(any[ServletRequest])(any[() => Unit])

我得到例外:

   The mock was not called as expected: 
    Invalid use of argument matchers!
    3 matchers expected, 2 recorded.
    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"));

我知道这是由最后一个无参数函数引起的。 如何在此处正确执行验证?

你能试试Function0[Unit]吗?

there was one(notifier).forward(any[ServletRequest])(any[Function0[Unit]])

相关内容

  • 没有找到相关文章