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();
}
}
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 .
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.
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).
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.
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.
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:
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.
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.
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.
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);
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With