ChangeStatusOnFailedStepsJobExecListener extends JobExecutionListenerSupport { @Override public void afterJob(JobExecution jobExecution) { List<StepExecution> failedStepExecutions = jobExecution.getStepExecutions() .stream() .filter(stepExecution -> stepExecution.getExitStatus().getExitCode().equals(ExitStatus.FAILED.getExitCode())) .collect(Collectors.toList()); if (failedStepExecutions.size() > 0) { jobExecution.setStatus(BatchStatus.FAILED); jobExecution.setExitStatus(new ExitStatus("FAILED-BECAUSE-OF-SKIPS")); } }  @Override void afterJob(JobExecution jobExecution);  }
@Test public void testAfterJobJobExecutionHasBatchStatusFailedWhenWeHaveStepsWithStatusNotCompleted() throws Exception { JobExecution jobExecution = new JobExecution(1L); StepExecution failedStepExecution = createStepExecution(jobExecution, FAILED); StepExecution completedStepExecution = createStepExecution(jobExecution, COMPLETED); StepExecution stoppedStepExecution = createStepExecution(jobExecution, STOPPED); jobExecution.addStepExecutions(asList(failedStepExecution, completedStepExecution, stoppedStepExecution)); changeStatusOnFailedStepsJobExecListener.afterJob(jobExecution); assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.FAILED); }
@Test public void testAfterJobJobExecutionHasBatchStatusUnchangedWhenAllStepsCompleted() throws Exception { JobExecution jobExecution = new JobExecution(1L); StepExecution completedStepExecution = createStepExecution(jobExecution, COMPLETED); StepExecution anotherStepExecution = createStepExecution(jobExecution, COMPLETED); jobExecution.addStepExecutions(asList(completedStepExecution, anotherStepExecution)); BatchStatus jobExecutionStatus = jobExecution.getStatus(); changeStatusOnFailedStepsJobExecListener.afterJob(jobExecution); assertThat(jobExecution.getStatus()).isEqualTo(jobExecutionStatus); }