gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:02:27 24 4
gpt4 key购买 nike

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

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

这是对模拟的验证:
val notifier = mock[Notifier]
there was one(notifier).forward(any[ServletRequest])(any[() => Unit])

我得到了异常(exception):
   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]])

关于scala - 匹配任何无参数函数作为 scala Mockito 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12693474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com