验证是否使用Mockito调用了方法而未指定参数

mus*_*oom 5 scala mockito specs2

使用Mockito,是否可以使用间谍或模拟来验证函数是否被调用/未调用,而不提供实际参数?例如,如果我有一个类或对象:

class MyClass{
  def f(x : Int) = x
}

object MyObject{
  def f(x : Int) = x
}
Run Code Online (Sandbox Code Playgroud)

我想能够说出类似的话:

val my_class = mock[MyClass]
// Do something that causes method f of MyClass to be called
there was one(my_class).f // Doesn't give arguments

val my_object = spy(MyObject)
// Do something that causes method f of MyObject to be called
there was one(my_object).f // Doesn't give arguments
Run Code Online (Sandbox Code Playgroud)

我只是想验证方法是否被调用,而不是它接收到特定的参数.此外,当我检查没有调用函数时:

there was no(my_object).f
Run Code Online (Sandbox Code Playgroud)

我不想验证它是不是用某些参数调用的,而是根本没有调用它.

有没有办法做到这一点?

rjs*_*ean 9

您可以使用Mockito匹配器指定您要验证是否对任何参数进行了调用.

在你的情况下将是

there was one (my_object/my_class).f(any[Int])
Run Code Online (Sandbox Code Playgroud)