Spring Batch中的步骤测试

时间:2018-12-26 19:36:28

标签: spring-batch batch-processing

使用stepLauncherTestUtils.launchstep()测试一个面向块的处理过程时,该过程包含读取器和写入器。

1。如何在一步测试的同时通过或设置工作浏览器?

步骤:

    @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返回值。

ThanksInAdavnce

1 个答案:

答案 0 :(得分:0)

  

如何检查Wheather读取器成功返回数据?

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

  

2。如何将作业执行ID和作业浏览器传递给以两个值作为参数的writer?

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