Reputation: 438
I'm attempting to test a Step contained within a Flow, contained in within a Job. Here's the basic Job configuration:
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();
I will make the flows more complex, they have been simplified for the sake of example. When I execute the following test I get an error:
JobParameters jobParameters = new JobParametersBuilder()
.addString("runId", "Step01").toJobParameters();
JobExecution exec = jobLauncherTestUtils
.launchStep("deleteProcessedRecords", jobParameters);
error:
java.lang.IllegalStateException: No Step found with name: [deleteProcessedRecords]
However, if I test the Job, the step is clearly there:
JobParameters jobParameters = new JobParametersBuilder()
.addString("runId", "Step01").toJobParameters();
JobExecution exec = jobLauncherTestUtils
.launchJob(jobParameters);
success:
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]
I've checked the documentation for Unit Testing which seem to indicate I should just be able to pass in the Step name. Can anyone show me how to properly test a Step or if I'm doing something incorrectly? Thanks in advance,
Sergio.
EDIT:
As requested, here's the step:
@Autowired
private DeleteProcessedRecordsTasklet deleteProcessedRecordsTasklet;
@Bean
public Step deleteProcessedRecords() {
return stepBuilderFactory.get("deleteProcessedRecords")
.tasklet(deleteProcessedRecordsTasklet)
.build();
}
I am using spring-batch-core version 3.0.7.RELEASE, part of spring-boot-starter-batch 1.3.5.RELEASE
Upvotes: 1
Views: 2267
Reputation: 6487
I had the same issue as you today and I hope this can help you.
My problem was that the flows were not initialized by the time that I was trying to launch the step, therefore the step could not be found:
java.lang.IllegalStateException: No Step found with name: [firstStep]
To Solve my issue I simply marked with the @Bean annotation the functions that return my Flows, this way the Flows are initialized before I try to launch the step using JobLauncherTestUtils.
@Bean //***this solved my problem***
public Flow initialFlow() {
return new FlowBuilder<Flow>("initialFlow")
.start(firstStep)
.next(secondStep)
.end();
}
Now I am able to run the step like this:
jobLauncherTestUtils.setJob(exampleJob);
JobExecution jobExecution = jobLauncherTestUtils.launchStep("firstStep");
I figured it out by placing a break point on this file inside the function findSteps
. I realized that the step was not being added to the map of steps that is used by the getStep
method(which is used by the launchStep
from JobLauncherTestUtils
).
My problem was not related to bug 2291 and I suspect yours is not either. If you are using 3.0.7.RELEASE you already have the code for the fix.
Upvotes: 1
Reputation: 16
Sorry, I would comment instead of answering, but I don't have enough reputation. But depending on your Spring batch version, you might be affected by this bug.
Upvotes: 0