gpt4 book ai didi

多个作业的 Spring Batch JUnit 测试

转载 作者:行者123 更新时间:2023-12-04 06:57:51 26 4
gpt4 key购买 nike

我在一个上下文文件中配置了两个作业

<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 进行单元测试时并测试工作启动它抛出异常说
No unique bean of type [org.springframework.batch.core.Job;] is defined: expected single matching bean but found 2: [JobA, JobB]

我尝试使用 @Qualifier对于 Autowiring 仍然是同样的事情。我哪里做错了

已编辑
@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());
}
}
}

异常(exception)是
No unique bean of type [org.springframework.batch.core.Job;] is defined: expected single matching bean but found 2: [JobA, JobB]`

最佳答案

也许晚了,

但我为自己找到了可行的解决方案:手动配置 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();
}

有了这个,您可以控制应为哪个 Job 应用 JobLauncherTestUtils。 (默认情况下,它需要上下文中的单个作业配置)

关于多个作业的 Spring Batch JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34217101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com