要点是 Spring Batch (v2) 测试框架具有JobLauncherTestUtils.setJob
与@Autowired
注解。我们的测试套件有多个Job
类提供者。由于这个类不是我可以修改的东西,我不确定如何限定它自动连接的作业,每个测试可能会有所不同。
STDOUT [WARN ] [2015.04.15 11:14:42] support.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobLauncherTestUtilsForSnapshot': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.test.JobLauncherTestUtils.setJob(org.springframework.batch.core.Job); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 2: coverageRuleBatch,generateMetricsSnapshotJob
我尝试添加这个可识别的 JavaConfig,但错误表明它仍在自动调用setJob
@Configuration
public class SpringTestConfiguration
{
@Bean
public JobLauncherTestUtils jobLauncherTestUtilsForSnapshot( final Job generateMetricsSnapshotJob )
{
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob( generateMetricsSnapshotJob );
return jobLauncherTestUtils;
}
}
注意:我不需要 JavaConfig 解决方案,但它会很好。另外,如果可能的话,我希望仍然保留像 JobRepository 这样的 Autowire 字段,因为只有一个字段。
我想出的解决方案
@Configuration
public class SpringBatchTestConfiguration
{
@Bean
public static JobLauncherTestUtils jobLauncherTestUtilsForSnapshot()
{
return new SnapshotJobLauncherTestUtils();
}
public static class SnapshotJobLauncherTestUtils extends JobLauncherTestUtils
{
@Override
@Qualifier( "generateMetricsSnapshotJob" )
public void setJob( final Job job )
{
super.setJob( job );
}
}
}
并在最终测试中
@Autowired
@Qualifier( "jobLauncherTestUtilsForSnapshot" )
protected JobLauncherTestUtils jobLauncherTestUtils;
我相当有信心可以用 @Component 注释我的 TestUtils 并正确命名它并做同样的事情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)