Search code examples
springjunitspring-batchspring-java-config

How to test a Step in a FlowJob


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


Solution

  • 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.