Spring Batch JUnit test for multiple jobs

前端 未结 5 2011
情深已故
情深已故 2020-12-16 20:24

I am having two jobs configured in one context file


        
            <         


        
相关标签:
5条回答
  • 2020-12-16 20:42

    Not answer to original problem, but using below code we have avoided reuse of JobLauncherTestUtils during sequential run of test cases in same class.

    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
    

    This indicates Junit to clean up and re construct context after each run.

    0 讨论(0)
  • 2020-12-16 20:43

    You have two similar beans declared in bean configuration file. To fix above problem, you need @Qualifier("JobA") and @Qualifier("JobB") to tell Spring about which bean should auto wired to which job.

    0 讨论(0)
  • 2020-12-16 20:44

    I solved it by creating JobLauncherTestUtils for each job separately (groovy):

    @TestConfiguration class BatchJobTestConfiguration {
    
    @Autowired
    @Qualifier('job1')
    private Job job1
    
    @Autowired
    @Qualifier('job2')
    private Job job2
    
    @Autowired
    JobRepository jobRepository;
    
    @Bean
    JobLauncher jobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        jobLauncher.setTaskExecutor(new SyncTaskExecutor());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }
    
    @Bean(name = 'jobLauncherTestUtilsJob1')
    JobLauncherTestUtils jobLauncherTestUtilsSyncEndUserJob() {
        new JobLauncherNoAutowireTestUtil(
                job: job1,
                jobLauncher: jobLauncher()
        )
    }
    
    @Bean(name = 'jobLauncherTestUtilsJob2')
    JobLauncherTestUtils jobLauncherTestUtilsenewCaseJob() {
        new JobLauncherNoAutowireTestUtil(
                job: job2,
                jobLauncher: jobLauncher()
        )
    }
    

    Then add this into your test:

    @ContextConfiguration(classes = [BatchJobTestConfiguration])
    ...
    @Autowired
    @Qualifier('jobLauncherTestUtilsJob1')
    private JobLauncherTestUtils jobLauncherTestUtils
    ...
    when:
    def jobExecution = jobLauncherTestUtils.launchJob()
    
    0 讨论(0)
  • 2020-12-16 20:58

    Maybe late,

    but I found for myself working solution: manual configuration of JobLauncherTestUtils:

    @Inject
    @Qualifier(value = "Job1")
    private Job job;
    
    @Inject
    private JobLauncher jobLauncher;
    
    @Inject
    private JobRepository jobRepository;
    
    private JobLauncherTestUtils jobLauncherTestUtils;
    
    private void initailizeJobLauncherTestUtils() {
        this.jobLauncherTestUtils = new JobLauncherTestUtils();
        this.jobLauncherTestUtils.setJobLauncher(jobLauncher);
        this.jobLauncherTestUtils.setJobRepository(jobRepository);
        this.jobLauncherTestUtils.setJob(job);
    }
    
    @Before
    public void setUp() throws Exception {
        this.initailizeJobLauncherTestUtils();
    }
    

    with this you can control for which Job should JobLauncherTestUtils be applied. (by default it expects single Job configuration in context)

    0 讨论(0)
  • 2020-12-16 20:58

    Because there is an @Autowired annotation on the setter for JobLauncherTestUtils.setJob(Job job) I had to use a MergedBeanDefinitionPostProcessor to set the property after the bean was created:

    @Configuration
    public class TestBatchConfiguration implements MergedBeanDefinitionPostProcessor {
    
        @Autowired
        @Qualifier("JobA")
        private Job job;
    
        @Bean(name="jtestl")
        public JobLauncherTestUtils jobLauncherTestUtils() {
            JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
            jobLauncherTestUtils.setJob(job);
            return jobLauncherTestUtils;
        }
    
        /**
         * https://stackoverflow.com/questions/22416140/autowire-setter-override-with-java-config
         * This is needed to inject the correct job into JobLauncherTestUtils
         */
        @Override
        public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
            if(beanName.equals("jtestl")) {
                beanDefinition.getPropertyValues().add("job", getMyBeanFirstAImpl());
            }
        }
    
        private Object getMyBeanFirstAImpl() {
            return job;
        }
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }
    }
    
    0 讨论(0)
提交回复
热议问题