Spring Batch Java configuration JobLauncherTestUtils

I am currently working on a spring loading project that uses a spring batch. I am trying to use JavaConfig instead of XML, but it is difficult with all the documents currently in XML.

I followed https://blog.codecentric.de/en/2013/06/spring-batch-2-2-javaconfig-part-5-modular-configurations, but I am having difficulty using it JobLauncherTestUtils. I know that I have to tell the test to use the correct spring context, but I cannot figure out how to do this. 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 is as follows:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class, MyJobConfiguration.class})
public class RetrieveDividendsTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void testSomething() throws Exception {
        jobLauncherTestUtils.launchJob();
    }

}
+6
source share
3

XML Spring Batch. , :

@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
    }
}

, ItemWriter , .

+9

pom.xml?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
</dependency>

, spring, beans spring, .

0

@SpringBatchTest Batch 4.1.x @SpringBatchTest . @SpringBatchTest, jobLauncherTestUtils, .

, 4.1.x

@Bean
     public JobLauncherTestUtils getJobLauncherTestUtils(){

            return new JobLauncherTestUtils() {
                @Override
                @Autowired
                public void setJob(@Qualifier("myjobname") Job job) {
                    super.setJob(job);
                }
            };
        }  
0
source

Source: https://habr.com/ru/post/1671761/


All Articles