jeudi 30 juillet 2020

Using a custom BatchConfigurer in Spring Batch tests with @SpringBatchTest annotation

In an application I am working on, we are using Spring Batch. Until now, we stored Spring Batch metadata in the same Oracle schema as the application data. Due to some requirements, we needed to split this. Piecing together Spring Batch documentation, I've managed to configure Spring Batch to use a custom DataSource for its metadata using this code in my main Configuration class:

@Configuration
@EnableBatchProcessing
@EnableConfigurationProperties
@ImportAutoConfiguration({ BatchAutoConfiguration.class })
public class ArchiverBatchConfiguration {
// ...
    @Bean
    public BatchConfigurer batchConfigurer(BatchDatabaseProperties batchDatabaseProperties) {
        // DataSource gets created and saved into the dataSource variable
        return new DefaultBatchConfigurer(dataSource);
    }
}

This setup worked. The code works well - the Job correctly pulls its data from the main DataSource used by Hibernate and Spring Batch saves the metadata into the DataSource specified in the BatchConfigurer. However, I am unable to run the existing tests for batch steps and other tools we have built on top of Spring Batch. The principal problem seems to be the fact, that in the tests, the JobLauncherTestUtils and JobRepositoryTestUtils that are @Autowired thanks to the @SpringBatchTest annotation, do not respect the DataSource set in my custom BatchConfigurer and instead use the primary DataSource of the application, which is supposed to only be used by Hibernate. The test classes are defined like this:

@RunWith(SpringRunner.class)
@SpringBatchTest
@SpringBootTest(classes = ArchiverBatchConfiguration.class)
public class ArchiveStepAcceptanceTest {
    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Autowired
    private JobRepositoryTestUtils jobRepositoryTestUtils;

    // And the actual tests follow...
}

Any idea on how to persuade the TestUtils beans to use the settings from the custom BatchConfigurer or about any possible workarounds?

Aucun commentaire:

Enregistrer un commentaire