Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Mar 18, 2015 · 2 comments
Labels
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

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

@spring-projects-issues
Copy link
Collaborator Author

Mariusz commented

Hi,
you can just write:

 // override method with name of your job bean
    @Bean
    public JobLauncherTestUtils getJobLauncherTestUtils() {

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

@spring-projects-issues spring-projects-issues added type: bug status: waiting-for-triage Issues that we did not analyse yet labels Dec 16, 2019
@fmbenhassine fmbenhassine removed the status: waiting-for-triage Issues that we did not analyse yet label Jan 15, 2021
@fmbenhassine fmbenhassine added this to the 5.0.0-M1 milestone Jan 15, 2021
@fmbenhassine fmbenhassine changed the title Do not autowired the Job inside JobLauncherTestUtils [BATCH-2366] Do not autowire the Job inside JobLauncherTestUtils Jan 15, 2021
@fmbenhassine fmbenhassine modified the milestones: 5.0.0-M1, 5.0.0 Jan 21, 2021
@fmbenhassine fmbenhassine added the has: votes Issues that have votes label May 3, 2022
@shaunvip
Copy link

shaunvip commented May 4, 2022

Seems this this one is throwing a circular bean issue.

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'MYJOB': Requested bean is currently in creation: Is there an unresolvable circular reference?

@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes= Application.class)
@SpringBootTest
@slf4j
public class MyJobITTest {

JobLauncherTestUtils jobLauncherTestUtils;

// override method with name of your job bean
@Bean
public JobLauncherTestUtils getJobLauncherTestUtils() {

    return new JobLauncherTestUtils() {
        @Override
        @Autowired
        public void setJob(@Qualifier(MYJOB) Job job) {
            super.setJob(job);
        }
    };
}

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

@fmbenhassine fmbenhassine modified the milestones: 5.0.0, 5.0.0-M5 Jun 8, 2022
@fmbenhassine fmbenhassine changed the title Do not autowire the Job inside JobLauncherTestUtils The job under test should not be autowired in JobLauncherTestUtils Aug 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants