7

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...

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.

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?

4

2 回答 2

9

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

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

于 2010-11-02T19:30:23.290 回答
4

您可以分别测试每个步骤。例子:

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

我希望它有所帮助。

于 2012-09-18T11:31:46.767 回答