>我正在尝试使用Mockito的间谍部分模拟服务,覆盖一种方法以使其返回一致的数据进行测试,但该间谍无缘无故地抛出未完成的StubbingException。
这是我的测试类:
@SpringBootTest
@RunWith(SpringRunner.class)
public class ApplicationIT {
private CompletableFuture<JobList> jobList;
@SpyBean
private Service serviceSpy;
@Before
public void setUp() {
initMocks(this);
jobList = new CompletableFuture<>();
jobList.complete(jobList.newBuilder()
.addAllJobs(jobTestData.getTestJob().getJobs()).build());
Mockito.doReturn(jobList).when(serviceSpy).fetchJob();
Mockito.doNothing().when(serviceSpy).reportSuccess(Mockito.any());
}
@Test
public void fetchJobCallTest() {
Mockito.verify(serviceSpy, timeout(60000).atLeastOnce()).fetchJob();
}
@Test
public void reportSuccessCallTest() {
Mockito.verify(serviceSpy, timeout(60000).atLeastOnce()).reportSuccess(Mockito.any());
}
}
两个测试都失败,org.mockito.exceptions.misusing.UnfinishedStubbingException
指向 Mockito.doReturn(jobList).when(serviceSpy).fetchJob();
at Mockito.doNothing().when(serviceSpy).reportSuccess(Mockito.any());
UnfinishedStubbingException means you are not mocking properly
this is not the right way of mocking a method... Mockito.doReturn(jobList).when(serviceSpy).fetchJob();
You can try below...
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();