Announcement Announcement Module
Collapse
No announcement yet.
Use Java Config and a ResourcelessTransactionManager for testing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use Java Config and a ResourcelessTransactionManager for testing

    Hi...

    I would like to use the @EnableBatchProcessing annotation to drive my automated testing process. Right now, it seems that a DataSource is required because of the reliance on the DefaultBatchConfigurer class. I don't want to use a Database, I'd rather perform the processing (testing) in memory using the ResourcelessTransactionManager and the MapJobRepository. So... I decided to not use the DefaultBatchConfigurer and implement my own. The Configuration that I came up with is listed below. The problem that I have with my configurations is that it is not clean. I have to call the 'afterPropertiesSet' on both the JobRepository and JobLauncher. I'd rather have the Spring Context do that for me. But I can't seem to find a way to have those beans be created by the context without a circular reference.

    Is there a simpler way to do this? What am I missing? Thanks!!!

    @Configuration
    @EnableBatchProcessing
    public class HelloWorldConfig implements BatchConfigurer {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    private final PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();

    private JobRepository jobRepository;

    private JobLauncher jobLauncher;

    @PostConstruct
    public void init() throws Exception {
    MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean();
    factory.setTransactionManager(transactionManager);
    factory.afterPropertiesSet();
    jobRepository = factory.getJobRepository();

    SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
    simpleJobLauncher.setJobRepository(jobRepository);
    simpleJobLauncher.afterPropertiesSet();
    jobLauncher = simpleJobLauncher;
    }

    @Bean
    public JobLauncherTestUtils jobLauncherTestUtils() {
    return new JobLauncherTestUtils();
    }

    @Bean
    public Job helloWorldJob() {
    return jobs.get("helloWorldJob").start(helloWorldStep()). build();
    }

    @Bean
    protected Step helloWorldStep() {
    return steps.get("helloWorldStep").tasklet(new HelloWorldTasklet())
    .build();
    }

    @Override
    public JobLauncher getJobLauncher() throws Exception {
    return jobLauncher;
    }

    @Override
    public JobRepository getJobRepository() throws Exception {
    return jobRepository;
    }

    @Override
    public PlatformTransactionManager getTransactionManager() throws Exception {
    return transactionManager;
    }

    }
Working...
X