JobScheduleController { public Date getNextFireTime() { List<? extends Trigger> triggers; try { triggers = scheduler.getTriggersOfJob(jobDetail.getKey()); } catch (final SchedulerException ex) { return null; } Date result = null; for (Trigger each : triggers) { Date nextFireTime = each.getNextFireTime(); if (null == nextFireTime) { continue; } if (null == result) { result = nextFireTime; } else if (nextFireTime.getTime() < result.getTime()) { result = nextFireTime; } } return result; }  void scheduleJob(final String cronExpression); void rescheduleJob(final String cronExpression); Date getNextFireTime(); void pauseJob(); void resumeJob(); void triggerJob(); void shutdown();  }
@Test public void assertGetNextFireTimeWhenSchedulerExceptionOccur() throws NoSuchFieldException, SchedulerException { ReflectionUtils.setFieldValue(jobScheduleController, "scheduler", scheduler); ReflectionUtils.setFieldValue(jobScheduleController, "jobDetail", jobDetail); JobKey jobKey = new JobKey("testJob"); when(jobDetail.getKey()).thenReturn(jobKey); doThrow(SchedulerException.class).when(scheduler).getTriggersOfJob(jobKey); assertNull(jobScheduleController.getNextFireTime()); }
@SuppressWarnings("unchecked") @Test public void assertGetNextFireTime() throws NoSuchFieldException, SchedulerException { ReflectionUtils.setFieldValue(jobScheduleController, "scheduler", scheduler); ReflectionUtils.setFieldValue(jobScheduleController, "jobDetail", jobDetail); JobKey jobKey = new JobKey("testJob"); Trigger trigger1 = mock(Trigger.class); Trigger trigger2 = mock(Trigger.class); Trigger trigger3 = mock(Trigger.class); Trigger trigger4 = mock(Trigger.class); @SuppressWarnings("rawtypes") List triggers = Arrays.asList(trigger1, trigger2, trigger3, trigger4); when(trigger1.getNextFireTime()).thenReturn(null); when(trigger2.getNextFireTime()).thenReturn(new Date(1L)); when(trigger3.getNextFireTime()).thenReturn(new Date(100L)); when(trigger4.getNextFireTime()).thenReturn(new Date(0L)); when(jobDetail.getKey()).thenReturn(jobKey); when(scheduler.getTriggersOfJob(jobKey)).thenReturn(triggers); assertThat(jobScheduleController.getNextFireTime().getTime(), is(0L)); }