Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch Java Config JobLauncherTestUtils

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();
    }

}
like image 255
Greg Potter Avatar asked Mar 08 '17 01:03

Greg Potter


People also ask

How do I disable Spring Batch meta-data table calls?

1 Answer. Show activity on this post. When you use @EnableBatchProcessing , by default Spring Batch will use the datasource you defined in your application context for its meta-data. If you want to use the map based job repository , you need to implement BatchConfigurer and override getJobRepositoy .

How do I run a specific job in Spring Batch?

Spring Batch auto configuration is enabled by adding @EnableBatchProcessing (from Spring Batch) somewhere in your context. By default it executes all Jobs in the application context on startup (see JobLauncherCommandLineRunner for details). You can narrow down to a specific job or jobs by specifying spring. batch.

What is Stepscope in Spring Batch?

The step scope means that Spring will create the bean only when the step asks for it and that values will be resolved then (this is the lazy instantiation pattern; the bean isn't created during the Spring application context's bootstrapping).

How do I check my Spring Batch job status?

You can get the status of the last job (so, the one running if there is one running) with http://localhost:8080/sprin-batch-admin-sample/batch/jobs/job1.json (replacing with job1 your actual job name and server). The parameter lastJobExecutionStatus should be STARTING , STARTED or STOPPING if it's still running.

How to run a Spring Batch job from the command line?

To run a Spring Batch Job, we require two things, the Job and a JobLauncher. It can contain the Job and JobLauncher in the same context or they can be part of different contexts. If we launch a Job from the command line, we will have a new JVM for each Job and each Job will have its own JobLauncher.

How to disable the default job launching configuration of Spring Boot batch?

Next, let's disable the default Job launching configuration of Spring Boot Batch by setting spring.batch.job.enabled=false in our application.properties. We configure our own JobLauncher to pass a custom JobParameters instance when launching the Job:

How to connect to Spring Batch project with Java configuration?

Click the Connect button to connect to it and we will see: On the left-hand side, we can see all the tables, we can use this as a normal SQL developer tool and run the queries to see the results. We will run the query for BATCH_JOB_EXECUTION table: In this post, we covered Spring batch project setup and configuration with Java configuration.

How do you test a Spring Batch job?

Testing the Spring Batch Job The spring-batch-test dependency provides a set of useful helper methods and listeners that can be used to configure the Spring Batch context during testing. Let's create a basic structure for our test: The @SpringBatchTest annotation provides the JobLauncherTestUtils and JobRepositoryTestUtils helper classes.


2 Answers

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.

like image 59
oschlueter Avatar answered Oct 17 '22 04:10

oschlueter


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);
                }
            };
        }  
like image 33
Niraj Sonawane Avatar answered Oct 17 '22 04:10

Niraj Sonawane