简体   繁体   中英

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:

在此处输入图片说明

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, eg 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?

您应该将显式类型提供给编译器

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM