Spring Batch Java Config JobLauncherTestUtils

17,024

Solution 1

I stumbled upon the same issue and had a look at this XML configuration from the Spring Batch samples. Based on that I managed to get it working with:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { BatchTest.BatchTestConfig.class })
public class BatchTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void demo() throws Exception {
        JobExecution jobExecution = jobLauncherTestUtils.launchJob();

        Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
    }

    @Configuration
    @EnableBatchProcessing
    static class BatchTestConfig {

        @Bean
        JobLauncherTestUtils jobLauncherTestUtils() {
            return new JobLauncherTestUtils();
        }

        // rest omitted for brevity
    }
}

The test succeeds and my ItemWriter logs the processed elements as expected.

Solution 2

For spring Batch 4.1.x or above Version We can use @SpringBatchTest annotation that will automatically inject jobLauncherTestUtils , check sample example for more details Here

This is how you can created if u cant upgrade to 4.1.x or above

@Bean
     public JobLauncherTestUtils getJobLauncherTestUtils(){

            return new JobLauncherTestUtils() {
                @Override
                @Autowired
                public void setJob(@Qualifier("myjobname") Job job) {
                    super.setJob(job);
                }
            };
        }  
Share:
17,024
Greg Potter
Author by

Greg Potter

Updated on June 06, 2022

Comments

  • Greg Potter
    Greg Potter almost 2 years

    I am currently working on a spring boot project that uses spring batch. I am trying to use JavaConfig instead of xml but it's difficult with all of the docs currently in xml.

    I followed https://blog.codecentric.de/en/2013/06/spring-batch-2-2-javaconfig-part-5-modular-configurations but am having difficulties using the JobLauncherTestUtils. I know I need to tell the test to use the correct spring context, but I can't seem to figure out how to do it. I get the following error:

    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.test.JobLauncherTestUtils' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    

    My test looks like the following:

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = {MyApplication.class, MyJobConfiguration.class})
    public class RetrieveDividendsTest {
    
        @Autowired
        private JobLauncherTestUtils jobLauncherTestUtils;
    
        @Test
        public void testSomething() throws Exception {
            jobLauncherTestUtils.launchJob();
        }
    
    }
    
  • Greg Potter
    Greg Potter about 7 years
    Yes I have the dependency. The code compiles just can't get the context correct.
  • Tom
    Tom about 7 years
    Spring boot use the following pattern to find your configuration in test environment: 1) search for the closest @SpringBootApplication in your test package. 2) search for the closest @SpringBootApplication in your main package. Do you have any of those? if not you can create one in your test pakcage with a @ComponentScan that search for your @Configuration files.
  • David Groomes
    David Groomes over 6 years
    For some reason with this approach I got "No qualifying bean of type 'org.springframework.batch.core.Job' available" until I moved/upped BatchTestConfig to its own class. A quirk of Spring I've somehow never encountered or a bug I don't know... Otherwise +1
  • Nicolas Widart
    Nicolas Widart over 5 years
    I have the same error, even after moving the BatchTestConfig to its own class. Getting Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionExcept‌​ion: No qualifying bean of type 'org.springframework.batch.core.Job' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
  • oschlueter
    oschlueter over 5 years
    @NicolasWidart the part // rest omitted for brevity contains a definition of a Job bean: @Bean public Job myJob(JobCompletionNotificationListener listener) { ... }. It sounds to me you're not configuring your Spring Batch pipeline properly.
  • Nicolas Widart
    Nicolas Widart over 5 years
    Oh I see, but then I'll have a duplicate of the job configuration which is already done in its config file no ?
  • oschlueter
    oschlueter over 5 years
    @NicolasWidart Can you load your non-test config in your test context and use the configured beans from there? Be mindful of duplicate beans of specific types (i.e. those you do need to have twice because you may need to change certain config steps for your test environment).