### Question:
JobScheduler { public void schedule(String jobName, SchedulingProperties schedulingProperties) { try { jobs.lastRun(jobName).ifPresent(this::restartIfFailed); } catch (NoSuchJobException | NoSuchJobExecutionException e) { throw new RuntimeException(e); } scheduler.schedule(() -> launchJob(jobName), new CronTrigger(schedulingProperties.getCron())); logger.info("Job {} scheduled for running. Cron: {}", jobName, schedulingProperties.getCron()); } @Inject  JobScheduler(TaskScheduler scheduler, Jobs jobs); void schedule(String jobName, SchedulingProperties schedulingProperties);  }### Answer:
@Test(expected = NoSuchJobException.class) public void testScheduleWhenJobNotExistsThrowsException() throws Exception { String jobName = "notFoundJob"; expect(jobs.lastRun(jobName)).andThrow(new NoSuchJobException()); replayAll(); jobScheduler.schedule(jobName, null); }
@Test public void testScheduleWhenNoPreviousExecutionExistsJobIsScheduled() throws Exception { String jobName = "job0"; SchedulingProperties schedulingProperties = new SchedulingProperties(); schedulingProperties.setCron("* * * * * ?"); expect(jobs.lastRun(jobName)).andReturn(Optional.empty()); expect(taskScheduler.schedule(isA(Runnable.class), eq(new CronTrigger(schedulingProperties.getCron())))).andReturn(scheduledFuture); replayAll(); jobScheduler.schedule(jobName, schedulingProperties); }
@Test public void testScheduleWhenPreviousExecutionWasSuccessfulJobIsScheduled() throws Exception { String jobName = "job0"; SchedulingProperties schedulingProperties = new SchedulingProperties(); schedulingProperties.setCron("* * * * * ?"); JobExecution jobExecution = new JobExecution(1L, new JobParameters()); jobExecution.setExitStatus(ExitStatus.COMPLETED); expect(jobs.lastRun(jobName)).andReturn(Optional.of(jobExecution)); expect(taskScheduler.schedule(isA(Runnable.class), eq(new CronTrigger(schedulingProperties.getCron())))).andReturn(scheduledFuture); replayAll(); jobScheduler.schedule(jobName, schedulingProperties); }
@Test public void testScheduleWhenPreviousExecutionFailedJobIsRestartedAndScheduled() throws Exception { String jobName = "job0"; SchedulingProperties schedulingProperties = new SchedulingProperties(); schedulingProperties.setCron("* * * * * ?"); JobExecution jobExecution = new JobExecution(1L, new JobParameters()); jobExecution.setExitStatus(ExitStatus.FAILED); expect(jobs.lastRun(jobName)).andReturn(Optional.of(jobExecution)); jobs.restart(1L); expectLastCall(); expect(taskScheduler.schedule(isA(Runnable.class), eq(new CronTrigger(schedulingProperties.getCron())))).andReturn(scheduledFuture); replayAll(); jobScheduler.schedule(jobName, schedulingProperties); }
@Test public void testScheduleWhenPreviousExecutionIsUnknownJobIsAbandonedAndScheduled() throws Exception { String jobName = "job0"; SchedulingProperties schedulingProperties = new SchedulingProperties(); schedulingProperties.setCron("* * * * * ?"); JobExecution jobExecution = new JobExecution(1L, new JobParameters()); jobExecution.setExitStatus(ExitStatus.UNKNOWN); expect(jobs.lastRun(jobName)).andReturn(Optional.of(jobExecution)); jobs.stopAndAbandon(1L); expectLastCall(); expect(taskScheduler.schedule(isA(Runnable.class), eq(new CronTrigger(schedulingProperties.getCron())))).andReturn(scheduledFuture); replayAll(); jobScheduler.schedule(jobName, schedulingProperties); }