使用Mockito匹配特定类型的空列表 [英] Use Mockito to match a null list of a specific type
问题描述
我有一个需要几个参数的方法.其中之一是某个类的列表,并且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());
其他说明:
- 在实践中,警告并不能保护您免受任何伤害;
null
是null
,并且通过类型擦除,所有这些形式无论如何都将编译为相同的字节码. - Java 8可以从方法参数中推断出泛型类型,因此
isNull()
在那里足够聪明而没有智能. - Mockito 1.x调用类
org.mockito.Matchers
,而Mockito 2.x弃用该类,而推荐使用org.mockito.ArgumentMatchers
.在这两种情况下,匹配器方法都可以通过org.mockito.Mockito
看到,但是静态继承方法在语义上很弱,并且可能导致这些方法无法在IDE中显示. -
如果没有
作为静态参数,就无法指定 < List< SomeClass>> isNull()
.如果您需要做很多事,您可以要做的就是提取一个本地静态方法...
- In practice, the warning isn't protecting you from anything;
null
isnull
, 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 oforg.mockito.ArgumentMatchers
. In both cases the matcher methods are visible throughorg.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 theMatchers
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.
这篇关于使用Mockito匹配特定类型的空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!