2015-12-26 73 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) 
    ); 
相關問題