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

Jon*_*tow 5 java testing spring-batch

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

不过,我想继续作业1的规格不变,因为这个测试是测试的配置,而不是基本的操作.我已经有验收测试来测试端到端的东西.这个例子是这样我就可以写为小的变化的目标测试,而对于每条边的情况下创建单独的终端到终端的测试.

我要测试的是,当内部第二步作业失败,step2.master会将我第4步,而不是第3步.有没有对此进行测试的好办法?

esm*_*lha 8

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

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

  • 您可以使用模拟实现替换复杂的作业,这些实现只提供下一个作业所需的任何内容(如果有任何依赖项).例如,假设step1读取数据库并在文件夹中生成文件.将模型替换为将测试文件移动到输出文件夹的模拟. (2认同)
  • 我会将您的测试分为"测试批处理流程的工作原理"和"分别测试每个步骤".要做测试#1,我会用模拟替换所有真正的步骤.要进行测试#2,我将使用Spring Batch Unit测试API,分别启动每个步骤. (2认同)