Skip to content

The job under test should not be autowired in JobLauncherTestUtils #1237

Closed
@spring-projects-issues

Description

@spring-projects-issues

Petar Tahchiev opened BATCH-2366 and commented

Hello,

I have several jobs defined. Then I want to test them so I create a bean of type JobLauncherTestUtils inside my config:

@Bean(name = "ncsvjobLauncherTestUtils")
public JobLauncherTestUtils defaultJobLauncherTestUtils() {
   JobLauncherTestUtils result =  new JobLauncherTestUtils();
   result.setJob(context.getBean("ncsvImportJob", Job.class));
   return result;
}

However when I run my tests I get this exception:

ption is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.test.JobLauncherTestUtils.setJob(org.springframework.batch.core.Job); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 3: ncsvImportJob,syncEntityJob,syncCatalogJob
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

This is because the JobLauncherTestUtils the setJob method has the @Autowired annotation so it will try to autowire the job, but as I said I have several.


2 votes, 3 watchers

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions