xen*_*ide 9 java spring spring-batch spring-3 spring-java-config
要点是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
Run Code Online (Sandbox Code Playgroud)
我已经尝试添加这个被识别的JavaConfig,但是错误说它仍然是自动调用的 setJob
@Configuration
public class SpringTestConfiguration
{
@Bean
public JobLauncherTestUtils jobLauncherTestUtilsForSnapshot( final Job generateMetricsSnapshotJob )
{
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob( generateMetricsSnapshotJob );
return jobLauncherTestUtils;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我不需要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 );
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在最终测试中
@Autowired
@Qualifier( "jobLauncherTestUtilsForSnapshot" )
protected JobLauncherTestUtils jobLauncherTestUtils;
Run Code Online (Sandbox Code Playgroud)
我相当有信心可以用 @Component 注释我的 TestUtils 并正确命名它并做同样的事情。
| 归档时间: |
|
| 查看次数: |
2619 次 |
| 最近记录: |