Verify that a method was called using Mockito without specifying arguments

mushroom picture mushroom · Aug 1, 2013 · Viewed 6.9k times · Source

Using Mockito, is it possible to verify that a function was called/not called using a spy or a mock, without giving the actual arguments? For example if I have a class or object:

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

object MyObject{
  def f(x : Int) = x
}

I want to be able to say something like:

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

I just want to verify that the method was called, not that it received specific arguments. Also, when I am checking that a function was not called:

there was no(my_object).f

I don't want to verify that it was not called with certain arguments, but that it was not called at all.

Is there any way to do this?

Answer

Ratan Sebastian picture Ratan Sebastian · Aug 1, 2013

You could use Mockito matchers to specify that you want to verify that the call was made for any argument.

in your case that would be

there was one (my_object/my_class).f(any[Int])