[英]Avoiding unchecked warnings when using Mockito
I'd like to mock a call on ScheduledExecutorService
to return mock of ScheduledFuture
class when method schedule
is invoked.我想模拟对
ScheduledExecutorService
的调用,以便在调用方法schedule
时返回ScheduledFuture
类的模拟。 The following code compiles and works correctly:以下代码编译并正常工作:
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
Mockito.when(executor.schedule(
Mockito.any(Runnable.class),
Mockito.anyLong(),
Mockito.any(TimeUnit.class))
).thenReturn(future); // <-- warning here
except that I'm getting unchecked warning in the last line:除了我在最后一行收到未经检查的警告:
found raw type: java.util.concurrent.ScheduledFuture
missing type arguments for generic class java.util.concurrent.ScheduledFuture<V>
unchecked method invocation: method thenReturn in interface org.mockito.stubbing.OngoingStubbing is applied to given types
required: T
found: java.util.concurrent.ScheduledFuture
unchecked conversion
required: T
found: java.util.concurrent.ScheduledFuture
Is it possible to somehow avoid these warnings?是否有可能以某种方式避免这些警告?
Code like ScheduledFuture<?> future = Mockito.mock(ScheduledFuture.class);
像
ScheduledFuture<?> future = Mockito.mock(ScheduledFuture.class);
这样的代码doesn't compile.不编译。
All warnings go away when alternative way of mock specification is used:当使用模拟规范的替代方式时,所有警告都会消失:
ScheduledFuture<?> future = Mockito.mock(ScheduledFuture.class);
Mockito.doReturn(future).when(executor).schedule(
Mockito.any(Runnable.class),
Mockito.anyLong(),
Mockito.any(TimeUnit.class)
);
Use the annotation on method level.在方法级别使用注释。
@SuppressWarnings("unchecked")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.