Spring Batch Java配置单元测试

huangapple go评论61阅读模式
英文:

Spring Batch Java configuration unit test

问题

Batch job/steps/beans can be configured using Java class in Spring Batch. Just curious do we need unit test this configuration Java class. I saw lots of talking about integration tests, but little on unit tests for Spring Batch. A sample Java class like below (from a Spring Batch book):

@Configuration
public class Demo9 {

private JobBuilderFactory jobBuilderFactory;
private StepBuilderFactory stepBuilderFactory;
private EmployeeProcessor employeeProcessor;
private DataSource dataSource;

@Autowired
public Demo9(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory, EmployeeProcessor employeeProcessor, DataSource dataSource) {
    this.jobBuilderFactory = jobBuilderFactory;
    this.stepBuilderFactory = stepBuilderFactory;
    this.employeeProcessor = employeeProcessor;
    this.dataSource = dataSource;
}

@Qualifier(value = "demo9")
@Bean
public Job demo9Job() throws Exception {
    return this.jobBuilderFactory.get("demo9")
            .start(step1Demo9())
            .build();
}

@Bean
public Step step1Demo9() throws Exception {
    return this.stepBuilderFactory.get("step1")
            .<EmployeeDTO, Employee>chunk(2)
            .reader(employeeReader())
            .processor(employeeProcessor)
            .writer(employeeDBWriterDefault())
            .build();
}

@Bean
@StepScope
Resource inputFileResource(@Value("#{jobParameters[fileName]}") final String fileName) throws Exception {
    return new ClassPathResource(fileName);
}

@Bean
@StepScope
public FlatFileItemReader<EmployeeDTO> employeeReader() throws Exception {
    FlatFileItemReader<EmployeeDTO> reader = new FlatFileItemReader<>();
    reader.setResource(inputFileResource(null));
    reader.setLinesToSkip(1);
    reader.setSkippedLinesCallback(new SkipRecordCallback());
    reader.setLineMapper(new DefaultLineMapper<EmployeeDTO>() {{
        setLineTokenizer(new FixedLengthTokenizer() {{
            setNames("employeeId", "firstName", "lastName", "email", "age");
            setColumns(new Range[]{new Range(1, 5), new Range(6, 10), new Range(11, 15), new Range(16, 30), new Range(31, 33)});
            setStrict(false);
        }});
        setFieldSetMapper(new EmployeeFileRowMapper());
    }});
    return reader;
}

@Bean
public JdbcBatchItemWriter<Employee> employeeDBWriterDefault() {
    JdbcBatchItemWriter<Employee> itemWriter = new JdbcBatchItemWriter<Employee>();
    itemWriter.setDataSource(dataSource);
    itemWriter.setSql("insert into employee (employee_id, first_name, last_name, email, age) values (:employeeId, :firstName, :lastName, :email, :age)");
    itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Employee>());
    return itemWriter;
}    

}

Want to have an idea we are possible to unit test this?

英文:

Batch job/steps/beans can be configured using Java class in Spring Batch. Just curious do we need unit test this configuration Java class. I saw lots of talking about integration tests, but little on unit tests for Spring Batch. A sample Java class like below (from a Spring Batch book):

@Configuration
public class Demo9 {

    private JobBuilderFactory jobBuilderFactory;
    private StepBuilderFactory stepBuilderFactory;
    private EmployeeProcessor employeeProcessor;
    private DataSource dataSource;

    @Autowired
    public Demo9(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory, EmployeeProcessor employeeProcessor, DataSource dataSource) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
        this.employeeProcessor = employeeProcessor;
        this.dataSource = dataSource;
    }

    @Qualifier(value = &quot;demo9&quot;)
    @Bean
    public Job demo9Job() throws Exception {
        return this.jobBuilderFactory.get(&quot;demo9&quot;)
                .start(step1Demo9())
                .build();
    }

    @Bean
    public Step step1Demo9() throws Exception {
        return this.stepBuilderFactory.get(&quot;step1&quot;)
                .&lt;EmployeeDTO, Employee&gt;chunk(2)
                .reader(employeeReader())
                .processor(employeeProcessor)
                .writer(employeeDBWriterDefault())
                .build();
    }

    @Bean
    @StepScope
    Resource inputFileResource(@Value(&quot;#{jobParameters[fileName]}&quot;) final String fileName) throws Exception {
        return new ClassPathResource(fileName);
    }

    @Bean
    @StepScope
    public FlatFileItemReader&lt;EmployeeDTO&gt; employeeReader() throws Exception {
        FlatFileItemReader&lt;EmployeeDTO&gt; reader = new FlatFileItemReader&lt;&gt;();
        reader.setResource(inputFileResource(null));
        reader.setLinesToSkip(1);
        reader.setSkippedLinesCallback(new SkipRecordCallback());
        reader.setLineMapper(new DefaultLineMapper&lt;EmployeeDTO&gt;() {{
            setLineTokenizer(new FixedLengthTokenizer() {{
                setNames(&quot;employeeId&quot;, &quot;firstName&quot;, &quot;lastName&quot;, &quot;email&quot;, &quot;age&quot;);
                setColumns(new Range[]{new Range(1, 5), new Range(6, 10), new Range(11, 15), new Range(16, 30), new Range(31, 33)});
                setStrict(false);
            }});
            setFieldSetMapper(new EmployeeFileRowMapper());
        }});
        return reader;
    }

    @Bean
    public JdbcBatchItemWriter&lt;Employee&gt; employeeDBWriterDefault() {
        JdbcBatchItemWriter&lt;Employee&gt; itemWriter = new JdbcBatchItemWriter&lt;Employee&gt;();
        itemWriter.setDataSource(dataSource);
        itemWriter.setSql(&quot;insert into employee (employee_id, first_name, last_name, email, age) values (:employeeId, :firstName, :lastName, :email, :age)&quot;);
        itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider&lt;Employee&gt;());
        return itemWriter;
    }    
}

Want to have an idea we are possible to unit test this?

答案1

得分: 1

I personally won't do that. This code is to configure Spring Batch (not the code of the business logic of the batch job itself). One could argue that this is code, so it should be tested. That is true, but pragmatically speaking, I would focus on testing the business logic of my batch job rather than the code of configuring Spring (Batch), which could have been an XML configuration file.

That code will be implicitly tested when you test your batch job. For example, the Spring application context won't start, or the job will fail to start because it is incorrectly configured, etc. You will notice these misconfigurations in tests, and I see no need to write a unit test to make sure that the employeeReader() method returns a bean of type FlatFileItemReader for example.

英文:

I personally won't do that. This code is to configure Spring Batch (not the code of the business logic of the batch job itself). One could argue that this is code, so it should be tested. That is true, but pragmatically speaking, I would focus on testing the business logic of my batch job rather than the code of configuring Spring (Batch), which could have been an XML configuration file.

That code will be implicitly tested when you test your batch job. For example, the Spring application context won't start, or the job will fail to start because it is incorrectly configured, etc. You will notice these misconfigurations in tests, and I see no need to write a unit test to make sure that the employeeReader() method returns a bean of type FlatFileItemReader for example.

huangapple
  • 本文由 发表于 2023年6月8日 02:39:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定