2017-05-04 123 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