问题描述
我在一个上下文文件中配置了两个作业
I am having two jobs configured in one context file
<batch:job id="JobA" restartable="true">
<batch:step id="abc">
<batch:tasklet >
<batch:chunk reader="reader" writer="writer" processor="processor" />
</batch:tasklet>
</batch:step>
</batch:job>
<batch:job id="JobB" restartable="true">
<batch:step id="abc">
<batch:tasklet >
<batch:chunk reader="reader" writer="writer" processor="processor" />
</batch:tasklet>
</batch:step>
</batch:job>
当我使用JobLauncherTestUtils
对JobA进行单元测试并测试作业启动时,会抛出异常
When i am doing unit testing for the JobA using JobLauncherTestUtils
and testing the job launch it is throwing an exception saying
No unique bean of type [org.springframework.batch.core.Job;] is defined: expected single matching bean but found 2: [JobA, JobB]
我尝试使用@Qualifier
进行自动接线仍然是一样的事情.我在这里哪里做错了
i tried using @Qualifier
for autowire still the same thing. Where am i doing wrong here
编辑
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:META-INF/spring/batch-test-context.xml" })
public class TestJob {
@Autowired
private JobExplorer jobExplorer;
@Autowired
@Qualifier("JobA")
private Job JobA;
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testJob() throws Exception {
JobParameters jobParameters = getNextJobParameters(getJobParameters());
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.getJobLauncher().run(JobA, jobParameters));
}
private JobParameters getJobParameters() {
JobParametersBuilder jobParameters = new JobParametersBuilder();
jobParameters.addString("param", "123");
return jobParameters.toJobParameters();
}
private JobParameters getNextJobParameters(JobParameters jobParameters) {
String jobIdentifier = jobLauncherTestUtils.getJob().getName();
List<JobInstance> lastInstances = jobExplorer.getJobInstances(jobIdentifier, 0, 1);
JobParametersIncrementer incrementer = jobLauncherTestUtils.getJob().getJobParametersIncrementer();
if (lastInstances.isEmpty()) {
return incrementer.getNext(jobParameters);
} else {
List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
return incrementer.getNext(lastExecutions.get(0).getJobParameters());
}
}
}
例外为
No unique bean of type [org.springframework.batch.core.Job;] is defined: expected single matching bean but found 2: [JobA, JobB]`
推荐答案
也许要晚了,
但是我为自己找到了可行的解决方案:JobLauncherTestUtils
的手动配置:
but I found for myself working solution: manual configuration of JobLauncherTestUtils
:
@Inject
@Qualifier(value = "Job1")
private Job job;
@Inject
private JobLauncher jobLauncher;
@Inject
private JobRepository jobRepository;
private JobLauncherTestUtils jobLauncherTestUtils;
private void initailizeJobLauncherTestUtils() {
this.jobLauncherTestUtils = new JobLauncherTestUtils();
this.jobLauncherTestUtils.setJobLauncher(jobLauncher);
this.jobLauncherTestUtils.setJobRepository(jobRepository);
this.jobLauncherTestUtils.setJob(job);
}
@Before
public void setUp() throws Exception {
this.initailizeJobLauncherTestUtils();
}
由此您可以控制应将JobLauncherTestUtils应用到哪个Job. (默认情况下,它期望在上下文中进行单个Job配置)
with this you can control for which Job should JobLauncherTestUtils be applied. (by default it expects single Job configuration in context)
这篇关于Spring Batch JUnit测试多个作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!