diff --git a/module/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/JobLauncherApplicationRunner.java b/module/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/JobLauncherApplicationRunner.java index 37b28c21864..b3c29f846a4 100644 --- a/module/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/JobLauncherApplicationRunner.java +++ b/module/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/JobLauncherApplicationRunner.java @@ -179,9 +179,8 @@ public class JobLauncherApplicationRunner if (this.jobRegistry != null && StringUtils.hasText(this.jobName)) { if (!isLocalJob(this.jobName)) { Job job = this.jobRegistry.getJob(this.jobName); - if (job != null) { - execute(job, jobParameters); - } + Assert.notNull(job, () -> "No job found with name '" + this.jobName + "'"); + execute(job, jobParameters); } } } diff --git a/module/spring-boot-batch/src/test/java/org/springframework/boot/batch/autoconfigure/JobExecutionExitCodeGeneratorTests.java b/module/spring-boot-batch/src/test/java/org/springframework/boot/batch/autoconfigure/JobExecutionExitCodeGeneratorTests.java index 0c250d0e926..91c0cb0f72b 100644 --- a/module/spring-boot-batch/src/test/java/org/springframework/boot/batch/autoconfigure/JobExecutionExitCodeGeneratorTests.java +++ b/module/spring-boot-batch/src/test/java/org/springframework/boot/batch/autoconfigure/JobExecutionExitCodeGeneratorTests.java @@ -29,7 +29,6 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link JobExecutionExitCodeGenerator}. * * @author Dave Syer - * @author Mahmoud Ben Hassine */ class JobExecutionExitCodeGeneratorTests { @@ -37,28 +36,29 @@ class JobExecutionExitCodeGeneratorTests { @Test void testExitCodeForRunning() { - JobInstance jobInstance = new JobInstance(1L, "job"); - JobExecution jobExecution = new JobExecution(1L, jobInstance, new JobParameters()); - this.generator.onApplicationEvent(new JobExecutionEvent(jobExecution)); + this.generator.onApplicationEvent(new JobExecutionEvent(testJobExecution())); assertThat(this.generator.getExitCode()).isOne(); } @Test void testExitCodeForCompleted() { - JobInstance jobInstance = new JobInstance(1L, "job"); - JobExecution jobExecution = new JobExecution(1L, jobInstance, new JobParameters()); - jobExecution.setStatus(BatchStatus.COMPLETED); - this.generator.onApplicationEvent(new JobExecutionEvent(jobExecution)); + JobExecution execution = testJobExecution(); + execution.setStatus(BatchStatus.COMPLETED); + this.generator.onApplicationEvent(new JobExecutionEvent(execution)); assertThat(this.generator.getExitCode()).isZero(); } @Test void testExitCodeForFailed() { - JobInstance jobInstance = new JobInstance(1L, "job"); - JobExecution jobExecution = new JobExecution(1L, jobInstance, new JobParameters()); - jobExecution.setStatus(BatchStatus.FAILED); - this.generator.onApplicationEvent(new JobExecutionEvent(jobExecution)); + JobExecution execution = testJobExecution(); + execution.setStatus(BatchStatus.FAILED); + this.generator.onApplicationEvent(new JobExecutionEvent(execution)); assertThat(this.generator.getExitCode()).isEqualTo(5); } + private static JobExecution testJobExecution() { + JobInstance jobInstance = new JobInstance(1L, "job"); + return new JobExecution(0L, jobInstance, new JobParameters()); + } + }