NoSuchBeanDefinitionException: Aucun bean qualifiant de type JobLauncherTestUtils

NoSuchBeanDefinitionException: aucun bean éligible de type JobLauncherTestUtils

Suivre lesofficial Spring batch unit testing guide pour créer un cas de test unitaire standard.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/job-abc.xml",
    "classpath:spring/batch/config/context.xml"})

public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

    }
}

P.S spring-batch-test.jar is added to the classpath.

Problème

Lorsqu'il est lancé au-dessus du test unitaire, il afficheJobLauncherTestUtils aucun message d'erreur de ce type de bean?

org.springframework.beans.factory.BeanCreationException: Could not autowire field:
    private org.springframework.batch.test.JobLauncherTestUtils com.example.AppTest.jobLauncherTestUtils;
    ......
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
    [org.springframework.batch.test.JobLauncherTestUtils] found for dependency:
    expected at least 1 bean which qualifies as autowire candidate for this dependency.
    ......
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
    ...

Solution

L'ajout despring-batch-test.jar dans le chemin de classe ne créera pas automatiquement le beanJobLauncherTestUtils.

Pour résoudre ce problème, déclare un beanJobLauncherTestUtils dans l'un de vos fichiers de configuration Spring.

spring/batch/config/test-context.xml



    

Et le charge dans le test unitaire.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/job-abc.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

    }
}