代码之家  ›  专栏  ›  技术社区  ›  Andrew Cheong

为Mockito.any()提供类以使verify()调用明确无误的正确语法是什么?

  •  0
  • Andrew Cheong  · 技术社区  · 7 年前

    我在用 Mockito.spy(...) 在非模拟对象上验证其方法之一从未被调用但是,有一点模棱两可,因为我只是用 any(), any() ,并且有两个重载具有两个参数:

    enter image description here

    我对Java还不太熟悉,无法找到正确的方式来表达Mockito对我的要求我想我不太懂Java中的反射概念, 例如 差异 Class , Function ,兰姆达斯等。

    这里有一个例子 实际的 (非Mockito)使用该方法:

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

    所以,我要验证的是第一个需要 Function<D, T> 第二个参数我试过但没用的东西:

    // 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?
    

    我怎样才能让它工作?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Dean Xu    7 年前

    你应该给编译器显式类型

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