Spring Batch Java Config JobLauncherTestUtils
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);
}
};
}
Greg Potter
Updated on June 06, 2022Comments
-
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 about 7 yearsYes I have the dependency. The code compiles just can't get the context correct.
-
Tom about 7 yearsSpring 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 over 6 yearsFor 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 over 5 yearsI have the same error, even after moving the BatchTestConfig to its own class. Getting
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.core.Job' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
-
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 over 5 yearsOh I see, but then I'll have a duplicate of the job configuration which is already done in its config file no ?
-
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).