[英]Mockito argument matcher that matches any of a type with generics, excluding nulls
Lets say I've got a class like this假设我有一堂这样的课
public class Wrapper<T> {
T data;
}
And I have a method call like this:我有一个这样的方法调用:
public interface ThingDoer {
<T> boolean doSomething(Wrapper<T> wrapper)
}
which I want to mock out in a test.我想在测试中模拟它。 Lets say we're all set up with the mockito stuff, and now I'm trying to mock out this method call
假设我们都设置了 mockito 的东西,现在我正在尝试模拟这个方法调用
when(thingDoer.doSomething(any(Wrapper.class))).thenReturn(true);
However, this will give me a warning: Unchecked assignment: 'package.Wrapper' to 'package.Wrapper<T>'
但是,这会给我一个警告:
Unchecked assignment: 'package.Wrapper' to 'package.Wrapper<T>'
I read another suggestion somewhere that with Java 8, you're supposed to use any()
instead of any(Wrapper.class)
.我在某处读到另一个建议,在 Java 8 中,你应该使用
any()
而不是any(Wrapper.class)
。 However, reading through the documentation on these two methods, any()
will accept null arguments and any(Class)
will reject null arguments, so they are not quite synonymous.但是,通读这两种方法的文档,
any()
将接受空参数,而any(Class)
将拒绝空参数,因此它们不是完全同义的。 Is there a way to exclude nulls without getting a warning?有没有办法在没有警告的情况下排除空值?
使用isNotNull()
作为匹配器。
when(thingDoer.doSomething(isNotNull())).thenReturn(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.