使用 Mockito 匹配特定类型的空列表

2022-01-14 00:00:00 unit-testing mockito java

我有一个需要几个参数的方法.其中之一是某个类的 List,null 是该参数的可接受值.

I have a method that takes a few parameters. One of them is a List of some class, and null is an acceptable value for this parameter.

public void doStuff(String string, @Nullable List<SomeClass> list) {
    ...
}

我想编写一个测试,使用 Mockito 来验证该方法是否以 null 作为参数调用.我尝试使用 isNull(List.class):

I want to write a test that uses Mockito to verify the method was called with null as a parameter. I tried using isNull(List.class):

MyClass myClass = ...
verify(myClass).doStuff(any(String.class), isNull(List.class));

但这会产生警告:

未经检查的转换
需要:java.util.List
com.package.SomeClass>
找到:java.util.List

unchecked conversion
required: java.util.List< com.package.SomeClass>
found: java.util.List

如果列表不为空,我可以看到如何修复此警告:

I can see how to fix this warning in the event that the list is not null:

// this generates the same warning
verify(myClass).doStuff(any(String.class), any(List.class));

// this does not generate the warning
verify(myClass).doStuff(any(String.class), Matchers.anyListOf(SomeClass.class)));

但是,我似乎找不到将这两种方法结合在一起的方法.或者找到一种替代方法来完成我想做的事情.(除了用注释来抑制警告)

However, I can't seem to find a way to combine these two approaches together. Or find an alternate approach that accomplishes what I am trying to do. (Other than suppressing the warning with an annotation)

推荐答案

一般的答案是使用显式的泛型方法参数.

The general answer is to use an explicit generic method argument.

verify(myClass).doStuff(any(String.class), Matchers.<List<SomeClass>>isNull());

补充说明:

  • 实际上,警告并不能保护您免受任何伤害;nullnull,并且通过类型擦除,所有这些形式都将编译为相同的字节码.
  • Java 8 可以从方法参数推断泛型类型,因此 isNull() 就足够了.
  • Mockito 1.x 调用类 org.mockito.Matchers,而 Mockito 2.x 弃用该类以支持 org.mockito.ArgumentMatchers.在这两种情况下,匹配器方法都可以通过 org.mockito.Mockito 看到,但 static-methods-via-inheritance 在语义上很弱,可能会导致这些方法不显示在 IDE 中.
  • 如果没有将 Matchers 作为静态参数,则无法指定 <List<SomeClass>>isNull().你可以做什么,如果你需要做很多,就是提取一个本地静态方法......

  • In practice, the warning isn't protecting you from anything; null is null, and with type erasure all of these forms will compile to the same bytecode anyway.
  • Java 8 can infer generic types from method arguments, so isNull() is sufficient there without cleverness.
  • Mockito 1.x calls the class org.mockito.Matchers whereas Mockito 2.x deprecates that class in favor of org.mockito.ArgumentMatchers. In both cases the matcher methods are visible through org.mockito.Mockito, but static-methods-via-inheritance is semantically weak and can cause those methods not to show up in IDEs.
  • There's no way to specify <List<SomeClass>>isNull() without the Matchers as a static argument. What you can do, if you need to do this a lot, is to extract a local static method...

private static List<SomeClass> nullSomeClassList() {
  return isNull();  // Return value types can be inferred before Java 8.
}

// elsewhere
verify(myClass).doStuff(any(String.class), nullSomeClassList());

...但无论你做什么,不要提取到字段.副作用在这里很重要.

...but whatever you do, don't extract to a field instead. The side effects are what matters here.

相关文章