### Question:
DeleteUserInfoCronJob { @Scheduled(cron = "0 0 6 * * *") public void deleteUserInfo() { LOGGER.info("Running cron job to delete users"); Iterable<CustomerInfo> customerInfoList = customerInfoRepo.findAll(); List<CustomerInfoCronJobInfo> customersToDelete = new ArrayList<>(); if (customerInfoList == null) { LOGGER.info("No Customers to Delete"); return; } customerInfoList.forEach(c -> { customersToDelete.add(new CustomerInfoCronJobInfo(c)); }); LocalDateTime now = LocalDateTime.now(); for (CustomerInfoCronJobInfo c : customersToDelete) { if (c.getCreatedAt().plus(NUM_DAYS_RETENTION, ChronoUnit.DAYS).isBefore(now)) { List<PrintJob> pJobs = printJobRepo.findPrintJobsByCustomerIdNewestFirst(c.getId()); if (pJobs != null && pJobs.size() >= 1) { PrintJob p = pJobs.get(0); if (p != null) { if (p.getCreatedAt().plus(NUM_DAYS_RETENTION, ChronoUnit.DAYS).isBefore(now)) { printJobRepo.deleteAll(pJobs); customerInfoRepo.deleteById(c.getId()); } } } } } } DeleteUserInfoCronJob(CustomerInfoRepo customerInfoRepo, PrintJobRepo printJobRepo); @Scheduled(cron = "0 0 6 * * *") void deleteUserInfo();  }### Answer:
@Test public void deleteUserInfo() { CustomerInfo ci = new CustomerInfo(); ci.setCreatedAt(LocalDateTime.of(2019, Month.JANUARY, 1, 0, 0)); List<CustomerInfo> customerInfoList = new ArrayList<>(); customerInfoList.add(ci); PrintJob p = new PrintJob(); p.setCreatedAt(LocalDateTime.of(2019, Month.JANUARY, 1, 0, 0)); List<PrintJob> pJobs = new ArrayList<>(); pJobs.add(p); when(customerInfoRepo.findAll()).thenReturn(customerInfoList); when(printJobRepo.findPrintJobsByCustomerIdNewestFirst(anyInt())).thenReturn(pJobs); doNothing().when(customerInfoRepo).deleteById(anyInt()); doNothing().when(printJobRepo).deleteAll(any()); cronJob.deleteUserInfo(); }
@Test public void deleteUserInfoNoOlderUsers() { List<CustomerInfo> customerInfoList = new ArrayList<>(); CustomerInfo ci = new CustomerInfo(); ci.setCreatedAt(LocalDateTime.now()); customerInfoList.add(ci); when(customerInfoRepo.findAll()).thenReturn(customerInfoList); cronJob.deleteUserInfo(); }
@Test public void deleteUserInfoNoUsers() { when(customerInfoRepo.findAll()).thenReturn(null); doNothing().when(customerInfoRepo).deleteById(anyInt()); doNothing().when(printJobRepo).deleteAll(any()); cronJob.deleteUserInfo(); }