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

谁说胖子不能爱 提交于 2019-12-03 15:53:11

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!