2016-12-12 24 views
1

我试图在使用JUnit的Spring引导中对Spring批处理作业进行单元测试。 我写这个测试类,我想窥探豆ItemReader:Spring引导@SpyBean尝试实例化一个新的bean,而不是在上下文中侦察这个bean

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment=WebEnvironment.NONE) 
@ActiveProfiles({"dev", "batch", "test-jobs"}) 
public class BatchJobTest { 

    @Autowired 
    private JobLauncherTestUtils jobLauncherTestUtils; 

    private @Autowired @Qualifier("contactDownloadAckJob") Job contactDownloadAckTaskJob; 

    @SpyBean 
    private ItemReader<CrsOscContact> reader; 

    @Test 
    public void testJob() throws Exception { 
     given(this.reader.read()).willReturn(new CrsOscContact()); 
     //... blah blah blah 
    } 

} 

当我运行这个测试,似乎@SpyBean标注没有完成其工作,应进行代理的ItemReader豆这已(正确的)例外,因为根据定义,如果没有找到bean,它会尝试实例化一个新类型的bean(并且我指定了一个接口):

org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.ItemReader]: Specified class is an interface 

我非常确定bean(ItemReader类型)已经在上下文中,因为:

  • 调试,我看到目标bean实例化正确处理
  • 如果我改变@SpyBean注释的@Autowired注解,先前点的情况下被正确注射

任何提示?谢谢

+0

在我开了Github上的问题的同时,因为刺探其他豆类就像一个魅力:https://github.com/spring-projects/spring-boot/issues/7625 – lincetto

回答

相关问题