Spring Batch JUnit test for multiple jobs

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

I am having two jobs configured in one context file


        
            <         


        
5条回答
  •  难免孤独
    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()
    

提交回复
热议问题