2017-07-03 49 views
1

下面的代码片段从一个单位的Mockito提取测试我的工作:当Mockito的“何时”有多次“返回”呼叫时,这意味着什么?

 Mockito.when(clientResource.getJobStatus(JOB_ID)) 
      .thenReturn(createJobStatus(JOB_ID, com.test.models.JobStatus.State.IDLE)) 
      .thenReturn(createJobStatus(JOB_ID, com.test.models.JobStatus.State.STARTING)) 
      .thenReturn(createJobStatus(JOB_ID, com.test.models.JobStatus.State.RUNNING)); 

是什么意思时,Mockito.when(...)有多个thenReturn(...)的后链接?

回答

1

这意味着它将按照它们声明的顺序返回这些值或answers

getJobStatus()的第一次调用返回createJobStatus(JOB_ID, com.test.models.JobStatus.State.IDLE)

第二个电话会返回createJobStatus(JOB_ID, com.test.models.JobStatus.State.STARTING)

等等......

你可以看到,在this methodanswers正在从queue调查。

+0

就这样,当相同的模拟方法被多次调用时,这些答案将是它按顺序点击的答案。知道了谢谢。 –

相关问题