首页 文章

在Spring-Batch中测试作业流的最佳方法是什么?

提问于
浏览
5

我有一个复杂的批处理应用程序,我想测试我对流程的假设是否正确 .

这是我正在使用的简化版本:

<beans>
  <batch:job id="job1">
    <batch:step id="step1" next="step2">
      <batch:tasklet ref="someTask1"/>
    </batch:step>
    <batch:step id="step2.master">
      <batch:partition partitioner="step2Partitioner"
            step="step2" />
      <batch:next on="*" to="step3" />
      <batch:next on="FAILED" to="step4" />
    </batch:step>
    <batch:step id="step3" next="step3">
      <batch:tasklet ref="someTask1"/>
    </batch:step>
    <batch:step id="step4" next="step4">
      <batch:tasklet ref="someTask1"/>
    </batch:step>
  </batch:job>
  <batch:job id="job2">
    <batch:step id="failingStep">
      <batch:tasklet ref="failingTasklet"/>
    </batch:step>
  </batch:job>

  <bean id="step2Partitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner" scope="step">
    <property name="resources" value="file:${file.test.resources}/*" />
  </bean>

  <bean id="step2" class="org.springframework.batch.core.step.job.JobStep">
    <property name="job" ref="job2" />
    <property name="jobLauncher" ref="jobLauncher" />
    <property name="jobRepository" ref="jobRepository" />
  </bean>
</beans>

Job1是我想要测试的工作 . 我真的只想测试step2.master到step3或step4的转换 . 我根本不想测试step1 ...

但是,我想保持Job1的规范不变,因为此测试是测试配置,而不是基础操作 . 我已经有验收测试来测试端到端的东西 . 这个例子是这样我可以编写针对小变化的目标测试,而无需为每个边缘情况创建单独的端到端测试 .

我想测试的是,当step2中的作业失败时,step2.master会将我转发到第4步而不是第3步 . 有没有一种好的方法来测试它?

2 回答

  • 3

    您可以单独测试每个步骤 . 例:

    JobLauncherTestUtil jobLauncherTestUtil = new JobLauncherTestUtil();
    jobLauncherTestUtil.setJobLauncher(jobLauncher);
    jobLauncherTestUtil.setJob(job);
    jobLauncherTestUtil.setJobRepository(jobRepository);
    Map<String, JobParameter> params = Maps.newHashMap();
    //determine job params here:
    params.put(....);
    JobParameters jobParams = new JobParameters(params);
    ExecutionContext context = new ExecutionContext();
    //put something to job context, if you need.
    context.put(...);
    JobExecution jobExecution = jobLauncherTestUtil.launchStep("stepId",jobParams,context);
    
    Assert.assertEquals("Step stepId failed", ExitStatus.COMPLETED, execution.getExitStatus())
    

    我希望它有所帮助 .

  • 8

    您可以使用始终失败的模拟实现替换step2,并使用StepExecutionListener检查是否调用了step3和step4 .

    这里有很好的例子:http://static.springsource.org/spring-batch/reference/html/testing.html#endToEndTesting

相关问题