首页 > 解决方案 > Spring Batch 中的步骤测试

问题描述

在使用 jobLauncherTestUtils.launchstep() 测试一个包含读取器和写入器的面向块的处理步骤时。

1.单步测试时如何通过或设置job explorer?

步:

    @Bean(name="step_name")
    public step step1(RepositoryItemReader<Person> perosnItemReader,
        FlatFileItemWriter<String> personItemWriter,
        TaskExecutor personTaskExecutor){
      return StepBuilderFactory
          .get("step_name")
          .listener(new listener())
          .<Person,String>Chunk(200)
          .reader(personItemReader)
          .writer(personItemWriter)
          .listener(new DateReadListener())
          .build();
    }

读者

@Bean(name="step_reader")
public RepositoryItemReader<Person> personItemReader(
    @Value("#jobParameters[id]}" String id){
  return new RepositoryItemReader<Person>()
        .name("step_reader")
        .repository(personDao)
        .methodName("getDetails")
        .arguments(id)
        .build();
}

作家

@Bean(name="step_writer")
public FlatFileItemWriter<String> personItemWriter(
    @Value("#{jobExecutionContext[person]}") Person person,
    @Value("#{stepExecution.jobExecution.id}" long id,
    JobExplorer jobExplorer) {

    Foo foo = new 
    Foo(jobExplorer.getJobExecution(jobId).getExecutionContext(),new 
    CurrentDate());
    return new FlatFileItemWriterBuilder()
        .get("item_writer")
        .callback(foo)
        .build();
  }

我测试它的方式。

ExecutionContext executionContext = new ExecutionContext()
executionContext.put("person",Person);  
JobExecution jobExecution =              
    JobLauncherTestUtils.launchStep("step_name",jobparams,executionContext);

问题

class Foo{
   Foo f = new Foo(JobExecution jobExection, Date date);
   Person person = (Person)jobExecution.getExecutionContext().get("person");
}

在这里,jobExecution 返回值。

提前感谢

标签: spring-batchbatch-processing

解决方案


我如何检查阅读器是否成功返回数据?

您无需测试整个步骤即可检查阅读器是否正确返回数据。阅读器本身的单元测试就足够了:创建并打开阅读器,调用read并断言结果。

2.如何将作业执行ID和作业资源管理器传递给以两个值作为参数的写入器?

为什么项目编写者需要作业执行和作业浏览器?从您的代码中,创建编写器时不使用这些。


推荐阅读