将任何无参数函数作为scala Mockito中的参数进行匹配
我正在尝试验证使用Mockito调用以下方法:将任何无参数函数作为scala Mockito中的参数进行匹配,scala,mocking,mockito,specs,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 wa
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"));
我知道这是由最后一个无参数函数引起的。如何在此处正确执行验证?您能否尝试
功能0[单元]
there was one(notifier).forward(any[ServletRequest])(any[Function0[Unit]])
=>Unit
不是一个函数类型,而是一个按名称的参数。@BenJames那么(onFailure:Unit)
与(onFailure:Unit)
有什么不同呢?区别在于执行代码块的时候。有一个参数forward(onFailure:Unit)是没有意义的。将在调用forward之前对参数进行求值。使用forward(onFailure:=>Unit)方法forward决定何时计算参数。方法定义和期望值不匹配。方法forward需要一个by-name参数,而期望的参数是一个具有零参数的函数。