2015-12-26 86 views
4

当方法schedule被调用时,我想嘲笑ScheduledExecutorService的调用返回ScheduledFuture类的模拟。此代码工作得很好:使用Mockito时避免未经检查的警告

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 

除了我得到选中警告在最后一行:

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 

是否有可能以某种方式避免这些警告?

ScheduledFuture<?> future = Mockito.mock(ScheduledFuture.class);这样的代码不能编译。

+0

是你的'ScheduledFuture'打算什么类型,在您的实际代码绑定到? – Makoto

+0

它是'ScheduledFuture ',因为我只提交'Runnable'。 –

+0

如果这个特殊的测试功能正常,那么我不会担心它太多。如果将类型绑定到“Object”,可能会删除警告,但我怀疑这是值得改变的。你能粘贴特定的测试和测试代码,以便我们可以在我们自己的环境中复制它吗?只要复制警告就行。 – Makoto

回答

2

所有警告消失在使用模拟规范的另一种方式:

ScheduledFuture<?> future = Mockito.mock(ScheduledFuture.class); 
    Mockito.doReturn(future).when(executor).schedule(
     Mockito.any(Runnable.class), 
     Mockito.anyLong(), 
     Mockito.any(TimeUnit.class) 
    ); 
相关问题