Testing of spring batch job causes unexpected results

wtsiamruk :

I'm trying to create a simple backup job processing, to verify that my goals are correct.

I'm trying to use spring batch testing, for results verification.

PS My batch processing uses non default configuration provided by framework, because our job repository should use non default schema name.

The read step of my job is configured for lazy initialization with @StepScope annotation, it is needed because my job should have some parameters to query the database on reading step

This is the sample configuration that we're using It is in the root package, rest batch configs located in children packages

@Configuration
@Import({ApplicationHibernateConfiguration.class})
@ComponentScan
public class ApplicationBatchConfiguration extends DefaultBatchConfigurer {

    private static final String BATCH_PROCESSING_PREFIX = "BATCH_PROCESSING.BATCH_";
    private final DataSource dataSource;
    private final PlatformTransactionManager transactionManager;
    private final JobLauncher jobLauncher;
    private final JobRepository jobRepository;
    private final JobExplorer jobExplorer;

    @Autowired
    public GlobalLogisticsPortalBatchConfiguration(
            DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception {
        this.dataSource = dataSource;
        this.transactionManager = transactionManager;
        this.jobRepository = createJobRepository();
        this.jobLauncher = createJobLauncher();
        this.jobExplorer = createJobExplorer();
    }


    @Override
    protected JobLauncher createJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

    @Override
    protected JobRepository createJobRepository() throws Exception {
        JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
        factoryBean.setDatabaseType("DB2");
        factoryBean.setTablePrefix(BATCH_PROCESSING_PREFIX);
        factoryBean.setIsolationLevelForCreate("ISOLATION_REPEATABLE_READ");
        factoryBean.setDataSource(this.dataSource);
        factoryBean.setTransactionManager(this.transactionManager);
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }

    @Override
    protected JobExplorer createJobExplorer() throws Exception {
        JobExplorerFactoryBean factoryBean = new JobExplorerFactoryBean();
        factoryBean.setDataSource(this.dataSource);
        factoryBean.setTablePrefix(BATCH_PROCESSING_PREFIX);
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }

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

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

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

    @Override
    @Bean
    public JobExplorer getJobExplorer() {
        return jobExplorer;
    }

    @Bean
    public JobBuilderFactory jobBuilderFactory(JobRepository jobRepository) {
        return new JobBuilderFactory(jobRepository);
    }

    @Bean
    public StepBuilderFactory stepBuilderFactory(
            JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilderFactory(jobRepository, transactionManager);
    }


}

Step that i'm trying to use looks like that:

    @Bean
    @StepScope
    public JdbcPagingItemReader<DomainObject> itemReader(
            @Value("#{jobParameters['id']}") String id) {
        JdbcPagingItemReader<DomainObject> reader = new JdbcPagingItemReader<>();
        reader.setDataSource(this.dataSource);
        reader.setFetchSize(10);
        Db2PagingQueryProvider nativeQueryProvider = new Db2PagingQueryProvider();
        nativeQueryProvider.setSelectClause("*");
        nativeQueryProvider.setFromClause("from SCHEMA.DOMAIN");
        nativeQueryProvider.setWhereClause("id = :id");
        Map<String, Object> params = new HashMap<>(1);
        params.put("id", id);
        reader.setRowMapper((rs, rowNum) -> {
            DomainObject element = new DomainObject();
            element.setId(rs.getString("ID"));
            return element;
        });
        reader.setParameterValues(params);
        reader.setQueryProvider(nativeQueryProvider);
        return reader;
    }

    @Bean
    public Step fetchDomain() throws Exception {
        return stepBuilderFactory.get("fetchDomain")
                .<HierarchyElement, HierarchyElement>chunk(10)
                .faultTolerant()
                .reader(itemReader(null))
                .writer(items -> items.forEach(System.out::println))
                .build();
    }

The actual job bean is currently configured to launch only single step

    @Bean
    public Job backupJob() throws Exception {
        return jobBuilderFactory.get("backupJob")
                .start(fetchHeid())
                .build();
    }

My Test code looks like following

@RunWith(SpringRunner.class)
@SpringBatchTest
@ContextConfiguration(classes = {ApplicationBatchConfiguration.class})
public class BackupJobConfigurationTest {
    @Autowired
    @Qualifier(value = "backupJob")
    public Job job;

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void flowTest() throws Exception {
        JobParameters parameters = new JobParametersBuilder()
                .addString("id", "124")
                .toJobParameters();

        JobExecution execution = jobLauncherTestUtils.launchJob(parameters);

        assertEquals(BatchStatuses.COMPLETED, execution.getExitStatus().getExitCode()); //failed
    }
}

I expect the exit code to be "COMPLETED" and get "UNKNOWN". Also I'm not sure that the code is actually getting even invoked because i don't see any outputs from the writer lambda.

The only output that i am seeing in test is

Aug 30, 2019 2:52:17 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=backupJob]] launched with the following parameters: [{id=124}]

org.junit.ComparisonFailure: 
Expected :COMPLETED
Actual   :UNKNOWN
wtsiamruk :

I am figured that out, first of all, i were need to remove SimpleAsyncTaskExecutor from my configuration to actually test the code in the same thread, then by more careful read of the reference I havve reconfigured my batch config, instead of extending BatchConfigurer directly I haveve configured it as a bean inside my configuration and add @EnableSpringBatch annotation

    @Bean
    public BatchConfigurer batchConfigurer() {
    return new DefaultBatchConfigurer() {
        @Override
        protected JobRepository createJobRepository() throws Exception {
            JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
            factoryBean.setDatabaseType("db2");
            factoryBean.setTablePrefix(BATCH_PROCESSING_PREFIX);
            factoryBean.setIsolationLevelForCreate("ISOLATION_REPEATABLE_READ");
            factoryBean.setDataSource(dataSource);
            factoryBean.setTransactionManager(transactionManager);
            factoryBean.afterPropertiesSet();
            return factoryBean.getObject();
        }

        @Override
        protected JobExplorer createJobExplorer() throws Exception {
            JobExplorerFactoryBean factoryBean = new JobExplorerFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setTablePrefix(BATCH_PROCESSING_PREFIX);
            factoryBean.afterPropertiesSet();
            return factoryBean.getObject();
        }

        @Override
        protected JobLauncher createJobLauncher() throws Exception {
            SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
            jobLauncher.setJobRepository(getJobRepository());
            //jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
            jobLauncher.afterPropertiesSet();
            return jobLauncher;
        }
    };
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Disable transactions in my Spring Batch job

分類Dev

Spring Batch Persist Job Meta Data

分類Dev

NoSuchJobException when running a job programmatically in Spring Batch

分類Dev

Spring Batch job execution status in response body

分類Dev

pass job parameters to custom writer Spring batch

分類Dev

Batch transform job results in "InternalServerError" with data file >100MB

分類Dev

Can I use Spring JdbcTemplate in Spring Batch Job Implementation?

分類Dev

Lambda causes unexpected token

分類Dev

Testing Batch SendAll ServiceStack

分類Dev

無限ループで実行されているSpring Batch Job

分類Dev

プログラムでSpring Batch Jobを実行しますか?

分類Dev

How to access execution context from a Spring Batch Step? Error: No context holder available for job scope

分類Dev

Does every job call in Spring Batch open a new database connection pool?

分類Dev

Deadlock while purging old records of spring batch job tables at scheduled time using shedlock

分類Dev

Spring Batchは常にSpring Bootで始まります:spring.batch.job.enabled = false動作しません

分類Dev

Adding dates in javascript with unexpected results

分類Dev

For loop producing unexpected results in C

分類Dev

Spring XD: pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, Object>)

分類Dev

How to update jsp status (from submitted to processing then success )if a spring batch job runing in background that will update a sql result status

分類Dev

useState() causes unexpected behaviour with audio object

分類Dev

Coroutine testing fails with "This job has not completed yet"

分類Dev

Unexpected null reference when testing with NUnit and Moq

分類Dev

Spring RestController + Junit Testing

分類Dev

Spring webflux testing

分類Dev

Spring Batch + Spring Boot + Couchbase

分類Dev

CallableTaskletAdapter Spring Batch

分類Dev

Spring Batch Writer

分類Dev

Spring Batch FlatFile Formatting

分類Dev

Spring Batch MultiLineItemReader with MultiResourcePartitioner

Related 関連記事

  1. 1

    Disable transactions in my Spring Batch job

  2. 2

    Spring Batch Persist Job Meta Data

  3. 3

    NoSuchJobException when running a job programmatically in Spring Batch

  4. 4

    Spring Batch job execution status in response body

  5. 5

    pass job parameters to custom writer Spring batch

  6. 6

    Batch transform job results in "InternalServerError" with data file >100MB

  7. 7

    Can I use Spring JdbcTemplate in Spring Batch Job Implementation?

  8. 8

    Lambda causes unexpected token

  9. 9

    Testing Batch SendAll ServiceStack

  10. 10

    無限ループで実行されているSpring Batch Job

  11. 11

    プログラムでSpring Batch Jobを実行しますか?

  12. 12

    How to access execution context from a Spring Batch Step? Error: No context holder available for job scope

  13. 13

    Does every job call in Spring Batch open a new database connection pool?

  14. 14

    Deadlock while purging old records of spring batch job tables at scheduled time using shedlock

  15. 15

    Spring Batchは常にSpring Bootで始まります:spring.batch.job.enabled = false動作しません

  16. 16

    Adding dates in javascript with unexpected results

  17. 17

    For loop producing unexpected results in C

  18. 18

    Spring XD: pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, Object>)

  19. 19

    How to update jsp status (from submitted to processing then success )if a spring batch job runing in background that will update a sql result status

  20. 20

    useState() causes unexpected behaviour with audio object

  21. 21

    Coroutine testing fails with "This job has not completed yet"

  22. 22

    Unexpected null reference when testing with NUnit and Moq

  23. 23

    Spring RestController + Junit Testing

  24. 24

    Spring webflux testing

  25. 25

    Spring Batch + Spring Boot + Couchbase

  26. 26

    CallableTaskletAdapter Spring Batch

  27. 27

    Spring Batch Writer

  28. 28

    Spring Batch FlatFile Formatting

  29. 29

    Spring Batch MultiLineItemReader with MultiResourcePartitioner

ホットタグ

アーカイブ