Search code examples
spring-batchjunit5

Testing Spring Batch 5 job


I am trying to test a Springbatch 5.0 job but I have still not figured out why I am getting this issue. I have looked at several documentations and suggestions on how to fix it but it has not helped me. So bringing it here for help.

ERROR o.s.b.c..AbstractStep [main] Exception while closing step execution resources in step someStep in job java.lang.IllegalStateException: No Scope registered for scope name 'step' at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java.java) ~{spring-beans-6.0.4.jar:6.0.4] at org.springframework.aop.target.SimpleBeanTargetSoiurce.getTarget(SimpleBeanTargetSource.java) ~{spring-aop-6.0.4.jar:6.0.4] at org.springframework.batch.item.file.FlatFileItemReader$$SpringCGLIB$$0.open() ~[spring-batch-infrastructure-5.0.0.jar:5.0.0]

@SpringBatchTest
@SpringJUnitConfig(Config.class)
public class TestClass {
    @Autowired
    private JobLauncherTestUtils jobUtils;

    @Test
    public void testMethod() throws Exception {
        JobParameters jobParameters = new JobParametersBuilder().addString("filename", "testname").toJobParameters;
        JobExecution jobExecution = jobUtils.launchJob(jobParameters);
        assertEquals(BatchStatus.COMPLETED,jobExecution.getStatus());

}

}

public class StepConfigClass {
//Autowired others

    public Step step(@Qualifier("writer") FlatFileItemWriter writer, @Qualifier("reader") FlatFileItemReader reader) {
        return new StepBuilder("testStep",jobRepository)
            .<obj,obj1>chunk(100,transactionManager)
            .reader(reader)
            .processor(processor)
            .writer(writer)
            .build();
    }
}


@StepScope
@Configuration
public class ReaderWriter {
    @value("#{jobParameters['filename']}")
    private String fileName;

    @StepScope
    @Bean
    public FlatFileItemReader<obj1> reader() {
        FlatFileItemReader<obj1> reader = new FlatFileItemReader<>();
        ...other configs
    }

   @StepScope
   @Bean
   public FlatFileItemWriter<obj2> writer() {
        FlatFileItemWriter<obj2> writer = new FlatFileItemWriter<>();
        writer.setFooterCallback(t -> t.write(fileName));
        ...other configs
   }

}

Solution

  • The @StepScope on the StepConfigClass adds no value, it should be removed.

    If you want to test step-scoped components, you need to make sure to have the StepScopeTestExecutionListener added on your test, see: Testing Step-Scoped Components. This should already be the case if you use @SpringBatchTest.

    EDIT: based on question edit and comment:

    After your edit, there is @StepScope on class ReaderWriter which is incorrect as well. You need to remove that too and move the injection of fileName where is it needed, meaning in the item writer bean definition:

    @Configuration
    public class ReaderWriter {
    
        @StepScope
        @Bean
        public FlatFileItemReader<obj1> reader() {
            FlatFileItemReader<obj1> reader = new FlatFileItemReader<>();
            ...other configs
        }
    
       @StepScope
       @Bean
       public FlatFileItemWriter<obj2> writer(@value("#{jobParameters['filename']}") String fileName) {
            FlatFileItemWriter<obj2> writer = new FlatFileItemWriter<>();
            writer.setFooterCallback(t -> t.write(fileName));
            ...other configs
       }
    
    }