Search code examples
javagenericsmockitoambiguous-call

What is the correct syntax to supply a class to Mockito.any() so that the verify() call is unambiguous?


I'm using Mockito.spy(...) on a non-Mock object to verify that one of its methods is never called. However, there's an ambiguity because I'm just using any(), any(), and there are two overloads with two parameters:

enter image description here

I'm a bit new to Java and can't figure out the right way to express what Mockito wants from me. I think I don't have a good handle on reflective concepts in Java, e.g. difference between Class, Function, lambdas, etc.

Here's an example of an actual (non-Mockito) use of that method:

        return jdbiExecutor.execute(Foo.class,
            foo -> {
              // Some code.
              return Bar.newBuilder().build();
            });

So, what I'm trying to verify is the first overload that takes a Function<D, T> for its second parameter. Some things I've tried but don't work:

// Is specifying just one of the parameters enough?
verifyZeroInteractions(executor.execute(any(Foo, any()));

// Maybe I need to supply the `.class()`?
verifyZeroInteractions(executor.execute(any(Foo.class, any()));

// Or literally, `Class<Foo>`?
verifyZeroInteractions(executor.execute(any(Class<Foo>), any()));

// Or what, do I _have_ to specify both parameters to some degree?

How can I get this to work?


Solution

  • You should give the explicit type to compiler

    executor.execute(any(), Matchers.<Function<?, ?>> any()); // here ? can be your explicit type