Mockito 无缘无故抛出 UnfinishedStubbingException
Mockito throws UnfinishedStubbingException for no apparent reason
我正在尝试编写一个“简单”的单元测试。然而,Mockito 总是告诉我有一个 UnfinishedStubbingException
.
Mockito 暴露为罪魁祸首的代码行如下:
when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId));
这是整个单元测试代码
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyServiceIdProcessorTest {
@Autowired
private MyServiceIdProcessor myServiceIdProcessor;
@MockBean
private MyServiceIdFactory myServiceIdFactory;
@Test
public void shouldFilterProductsWithNoId() {
Product productWithNoId = new Product();
MyServiceId emptyId = new MyServiceId();
when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId));
CatalogDTO catalogDTO = new CatalogDTO();
Envelope<CatalogDTO, Product> envelopeToTest = Envelope.products(List.of(productWithNoId));
Envelope returnedEnvelope = myServiceIdProcessor.enrichCatalog(envelopeToTest);
assertThat(returnedEnvelope.getProducts()).hasSize(0);
}
}
在定义 bahavoir
之前尝试 spy()
MyServiceIdFactory
@Test
public void shouldFilterProductsWithNoId() {
// Arrange
Product productWithNoId = new Product();
MyServiceId emptyId = new MyServiceId();
MyServiceIdFactory spyMyServiceIdFactory = spy(myServiceIdFactory);
SortedSet<MyServiceId> set = (SortedSet<MyServiceId>) Set.of(emptyId);
doReturn(set).when(spyMyServiceIdFactory).get(any());
CatalogDTO catalogDTO = new CatalogDTO();
Envelope<CatalogDTO, Product> envelopeToTest = Envelope.products(List.of(productWithNoId));
// Act
Envelope returnedEnvelope = spyMyServiceIdFactory.enrichCatalog(envelopeToTest);
// Assert
assertThat(returnedEnvelope.getProducts()).hasSize(0);
}
问题是以下转换异常:
when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId));
Set.of(foo)
无法转换为 SortedSet
。然而,异常似乎被 Mockito
吞没并覆盖
我正在尝试编写一个“简单”的单元测试。然而,Mockito 总是告诉我有一个 UnfinishedStubbingException
.
Mockito 暴露为罪魁祸首的代码行如下:
when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId));
这是整个单元测试代码
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyServiceIdProcessorTest {
@Autowired
private MyServiceIdProcessor myServiceIdProcessor;
@MockBean
private MyServiceIdFactory myServiceIdFactory;
@Test
public void shouldFilterProductsWithNoId() {
Product productWithNoId = new Product();
MyServiceId emptyId = new MyServiceId();
when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId));
CatalogDTO catalogDTO = new CatalogDTO();
Envelope<CatalogDTO, Product> envelopeToTest = Envelope.products(List.of(productWithNoId));
Envelope returnedEnvelope = myServiceIdProcessor.enrichCatalog(envelopeToTest);
assertThat(returnedEnvelope.getProducts()).hasSize(0);
}
}
在定义 bahavoir
之前尝试spy()
MyServiceIdFactory
@Test
public void shouldFilterProductsWithNoId() {
// Arrange
Product productWithNoId = new Product();
MyServiceId emptyId = new MyServiceId();
MyServiceIdFactory spyMyServiceIdFactory = spy(myServiceIdFactory);
SortedSet<MyServiceId> set = (SortedSet<MyServiceId>) Set.of(emptyId);
doReturn(set).when(spyMyServiceIdFactory).get(any());
CatalogDTO catalogDTO = new CatalogDTO();
Envelope<CatalogDTO, Product> envelopeToTest = Envelope.products(List.of(productWithNoId));
// Act
Envelope returnedEnvelope = spyMyServiceIdFactory.enrichCatalog(envelopeToTest);
// Assert
assertThat(returnedEnvelope.getProducts()).hasSize(0);
}
问题是以下转换异常:
when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId));
Set.of(foo)
无法转换为 SortedSet
。然而,异常似乎被 Mockito