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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4080735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:23:10  来源:igfitidea点击:

What is the best way to test job flow in Spring-Batch?

javatestingspring-batch

提问by Jon Bristow

I've got a complicated batch application, and I want to test that my assumptions about flow are correct.

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

Here's a much simplified version of what I'm working with:

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

<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 is the job I want to test. I really only want to test the transition of step2.master to step3 or step4. I don't want to test step1 at all...

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

However, I want to keep Job1's specification intact, since this test is testing the configuration, not the underlying actions. I already have acceptance tests to test end-to-end stuff. This example is so I can write targeted tests for small variations without creating seperate end-to-end tests for each edge case.

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

What I want to test is that when the job inside step2 fails, step2.master will forward me on to step 4 and not step 3. Is there a good way to test this?

我要测试的是,当 step2 中的作业失败时,step2.master 会将我转到第 4 步而不是第 3 步。有没有好的方法来测试这个?

采纳答案by esmiralha

You can replace step2 with a mock implementation that always fail and use a StepExecutionListener to check whether or not step3 and step4 were called.

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

There are good examples here: http://static.springsource.org/spring-batch/reference/html/testing.html#endToEndTesting

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

回答by Denis

You can test every step separately. Example:

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

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())

I hope it helps.

我希望它有帮助。