gpt4 book ai didi

java - Mockito SpyBean 抛出 UnfinishedStubbingException

转载 作者:行者123 更新时间:2023-11-30 12:06:30 35 4
gpt4 key购买 nike

我正在尝试使用 Mockito 的 Spy 部分模拟服务,覆盖一个方法以使其返回一致的数据以进行测试,但说 spy 无缘无故抛出 UnfinishedStubbingException。

这是我的测试类:

@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();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();

关于java - Mockito SpyBean 抛出 UnfinishedStubbingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55531932/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com