2017-05-04 68 views
1

在我的單元測試中,我將ScheduledExecutoryService類的模擬實例注入到了要嘗試測試的類中,以便在調用scheduleAtFixedRate(...)方法時,它會返回一個模擬的Future。不過出於某種原因,它總是返回null。有任何想法嗎 ?Mocking ScheduledExecutorService.scheduleWithFixedDelay(...)返回null

施藥代碼:

Future<?> t = 
scheduledExecutorService.scheduleAtFixedRate(this, 10, 10, TimeUnit.SECONDS); 

測試代碼:

@Mock ScheduledExecutorService scheduledExecutorService; 
@Mock ScheduledFuture<?> t; 

Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay(
any(Runnable.class), anyLong(), anyLong(), any(TimeUnit.class)); 
+2

scheduleAtFixedRate VS scheduleWithFixedDelay ...? –

+0

就是這樣。接得好。 – kpatelio

回答

2

你逝去的整數(也可能是方法參數的定義),雖然你期待龍的值。

更改爲:

Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay(
any(Runnable.class), anyInt(), anyInt(), any(TimeUnit.class)); 
+0

我已經將參數更改爲很長,但仍然看到問題。 – kpatelio