### Question:
TaskResultStatisticJob extends AbstractStatisticJob { @Override public void execute(final JobExecutionContext context) throws JobExecutionException { Optional<TaskResultStatistics> latestOne = repository.findLatestTaskResultStatistics(statisticInterval); if (latestOne.isPresent()) { fillBlankIfNeeded(latestOne.get()); } TaskResultStatistics taskResultStatistics = new TaskResultStatistics( sharedData.getSuccessCount(), sharedData.getFailedCount(), statisticInterval, StatisticTimeUtils.getCurrentStatisticTime(statisticInterval)); log.debug("Add taskResultStatistics, statisticInterval is:{}, successCount is:{}, failedCount is:{}", statisticInterval, sharedData.getSuccessCount(), sharedData.getFailedCount()); repository.add(taskResultStatistics); sharedData.reset(); }  @Override JobDetail buildJobDetail(); @Override Trigger buildTrigger(); @Override Map<String, Object> getDataMap(); @Override void execute(final JobExecutionContext context);  }### Answer:
@Test public void assertExecuteWhenRepositoryIsEmpty() throws SchedulerException { Optional<TaskResultStatistics> latestOne = Optional.absent(); for (StatisticInterval each : StatisticInterval.values()) { taskResultStatisticJob.setStatisticInterval(each); when(repository.findLatestTaskResultStatistics(each)).thenReturn(latestOne); when(repository.add(any(TaskResultStatistics.class))).thenReturn(true); taskResultStatisticJob.execute(null); verify(repository).findLatestTaskResultStatistics(each); } verify(repository, times(3)).add(any(TaskResultStatistics.class)); }
@Test public void assertExecute() throws SchedulerException { for (StatisticInterval each : StatisticInterval.values()) { taskResultStatisticJob.setStatisticInterval(each); Optional<TaskResultStatistics> latestOne = Optional.of(new TaskResultStatistics(0, 0, each, StatisticTimeUtils.getStatisticTime(each, -3))); when(repository.findLatestTaskResultStatistics(each)).thenReturn(latestOne); when(repository.add(any(TaskResultStatistics.class))).thenReturn(true); taskResultStatisticJob.execute(null); verify(repository).findLatestTaskResultStatistics(each); } verify(repository, times(StatisticInterval.values().length * 3)).add(any(TaskResultStatistics.class)); }