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

时间:2012-10-02 15:32:16

标签: scala mocking mockito specs

我正在尝试验证使用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"));

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

1 个答案:

答案 0 :(得分:2)

你能试试Function0[Unit]吗?

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