Spring 如何测试FlowJob中的步骤
我试图测试一个包含在流中的步骤,一个包含在作业中的步骤。以下是基本的作业配置:Spring 如何测试FlowJob中的步骤,spring,junit,spring-batch,spring-java-config,Spring,Junit,Spring Batch,Spring Java Config,我试图测试一个包含在流中的步骤,一个包含在作业中的步骤。以下是基本的作业配置: Flow readFlow = new FlowBuilder<Flow>("readFlow").start(step01.deleteProcessedRecords()).on("*") .to(step02.retrieveIdentifiers())...end(); Flow firstWriteFlow = new FlowBuilder<Fl
Flow readFlow = new FlowBuilder<Flow>("readFlow").start(step01.deleteProcessedRecords()).on("*")
.to(step02.retrieveIdentifiers())...end();
Flow firstWriteFlow = new FlowBuilder<Flow>("firstWriteFlow").from(step04.createFirstFile()).end();
FlowJobBuilder builder = new JobBuilder("createFiles").repository(jobRepository)
.incrementer(new RunIdIncrementer())
.start(readFlow)
.on("*")
.to(firstWriteFlow)
.end();
错误:
java.lang.IllegalStateException: No Step found with name: [deleteProcessedRecords]
但是,如果我测试作业,步骤显然就在那里:
JobParameters jobParameters = new JobParametersBuilder()
.addString("runId", "Step01").toJobParameters();
JobExecution exec = jobLauncherTestUtils
.launchJob(jobParameters);
成功:
2016-05-09 17:34:47.387 INFO 16748 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=createFiles]] launched with the following parameters: [{runId=Step01}]
2016-05-09 17:34:47.450 INFO 16748 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [deleteProcessedRecords]
2016-05-09 17:34:47.699 INFO 16748 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [retrieveIdentifiers]
我已经检查了文档,这些文档似乎表明我应该能够传入步骤名称。有人能告诉我如何正确地测试一个步骤,或者如果我做得不正确吗?提前感谢,
塞吉奥
编辑:
根据要求,以下是步骤:
@Autowired
private DeleteProcessedRecordsTasklet deleteProcessedRecordsTasklet;
@Bean
public Step deleteProcessedRecords() {
return stepBuilderFactory.get("deleteProcessedRecords")
.tasklet(deleteProcessedRecordsTasklet)
.build();
}
我使用的是spring batch core版本3.0.7.RELEASE,是spring boot starter版本1.3.5.RELEASE的一部分对不起,我想发表评论而不是回答,但我没有足够的声誉。但是,根据您的Spring批处理版本,您可能会受到影响。我今天遇到了与您相同的问题,希望这能对您有所帮助 我的问题是,在我尝试启动步骤时,流没有初始化,因此找不到步骤: java.lang.IllegalStateException:未找到名为:[firstStep]的步骤 为了解决我的问题,我只是用@Bean注释标记了返回流的函数,这样在我尝试使用JobLauncherTestUtils启动步骤之前,流就被初始化了
@Bean //***this solved my problem***
public Flow initialFlow() {
return new FlowBuilder<Flow>("initialFlow")
.start(firstStep)
.next(secondStep)
.end();
}
我通过在函数findSteps
内部放置一个断点来解决这个问题。我意识到该步骤没有被添加到getStep
方法使用的步骤映射中(该方法由JobLauncherTestUtils
中的launchStep
使用)
我的问题与你无关,我怀疑你的问题也与你无关。如果您使用的是3.0.7.RELEASE,那么您已经有了修复程序的代码。请添加步骤01中的代码。deleteProcessedRecords()感谢您的建议。我检查了POM,我使用的是spring boot starter batch version 1.3.3.RELEASE,它使用的是spring batch core 3.0.6.RELEASE,所以应该已经解决了。我将尝试升级到最新和最好的版本,看看会发生什么。很遗憾,结果相同。无法重新创建问题。我将把这个标记为答案。
@Bean //***this solved my problem***
public Flow initialFlow() {
return new FlowBuilder<Flow>("initialFlow")
.start(firstStep)
.next(secondStep)
.end();
}
jobLauncherTestUtils.setJob(exampleJob);
JobExecution jobExecution = jobLauncherTestUtils.launchStep("firstStep");