Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito无缘无故抛出UnfinishedStubbingException_Java_Unit Testing_Mockito - Fatal编程技术网

Java Mockito无缘无故抛出UnfinishedStubbingException

Java Mockito无缘无故抛出UnfinishedStubbingException,java,unit-testing,mockito,Java,Unit Testing,Mockito,我正在尝试编写一个“简单”的单元测试。然而,Mockito总是告诉我有一个未完成的存根异常 代码行Mockito作为罪魁祸首公开如下: when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId)); when(myServiceIdFactory.get(any()).thenReturn((SortedSet)Set.of(emptyId)); 下面是整个单元测试

我正在尝试编写一个“简单”的单元测试。然而,Mockito总是告诉我有一个
未完成的存根异常

代码行Mockito作为罪魁祸首公开如下:

when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId));
when(myServiceIdFactory.get(any()).thenReturn((SortedSet)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);
  }
}
@SpringBootTest
@RunWith(SpringRunner.class)
公共类MyServicedProcessorTest{
@自动连线
私有MyServiceIdProcessor MyServiceIdProcessor;
@蚕豆
私有MyServiceIdFactory MyServiceIdFactory;
@试验
public void应为FilterProductswithnoid(){
productWithNoId=新产品();
MyServiceId emptyId=新的MyServiceId();
当(myServiceIdFactory.get(any()).thenReturn((SortedSet)Set.of(emptyId));
CatalogDTO CatalogDTO=新CatalogDTO();
信封信封测试=信封产品(列表(产品无标识));
信封returnedDevelope=myServiceIdProcessor.enrichCatalog(信封测试);
资产(returnedDevelope.getProducts()).hasSize(0);
}
}
在定义bahavoir之前,尝试
spy()
myservicedfactory

  @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);
  }
@测试
public void应为FilterProductswithnoid(){
//安排
productWithNoId=新产品();
MyServiceId emptyId=新的MyServiceId();
MyServiceIdFactory spyMyServiceIdFactory=spy(MyServiceIdFactory);
SortedSet set=(SortedSet)集合的(emptyId);
doReturn(set).when(spyMyServiceIdFactory).get(any());
CatalogDTO CatalogDTO=新CatalogDTO();
信封信封测试=信封产品(列表(产品无标识));
//表演
信封返回开发=SpymyServicedFactory.enrichCatalog(信封测试);
//断言
资产(returnedDevelope.getProducts()).hasSize(0);
}

问题在于以下强制转换异常:

 when(myServiceIdFactory.get(any())).thenReturn((SortedSet<MyServiceId>) Set.of(emptyId));
when(myServiceIdFactory.get(any()).thenReturn((SortedSet)Set.of(emptyId));

Set.of(foo)
不能强制转换为
SortedSet
。但是,这个异常似乎被Mockito吞并并覆盖了

您可以共享MyServiceIdFactory接口吗?