### Question:
PendingFundCleaner { @Scheduled(fixedDelay = 300_000 ) public void cleanupPendingFunds() { pendingFundService.findAll() .stream() .filter(fund -> { try { return hasFailed(fund) || hasAged(fund); } catch (final Exception ex) { log.error("Unable to fetch status for transaction {} : ", fund.getTransactionHash(), ex.getMessage()); return false; } }) .forEach(pendingFundService::removePendingFund); } PendingFundCleaner(final PendingFundService pendingFundService,
                              final AzraelClient azraelClient); @Scheduled(fixedDelay = 300_000 /* 5 minutes */) void cleanupPendingFunds();  }### Answer:
@Test public void shouldRemoveFailedPendingFunds() { final PendingFund pendingFund = PendingFundMother.aPendingFund(); when(pendingFundService.findAll()) .thenReturn(of(pendingFund)); when(azraelClient.getTransactionStatus(prettify(pendingFund.getTransactionHash()))).thenReturn(TransactionStatus.FAILED); pendingFundCleaner.cleanupPendingFunds(); verify(pendingFundService).removePendingFund(pendingFund); }
@Test public void shouldRemoveAgedPendingFunds() { final PendingFund pendingFund = PendingFundMother.aPendingFund(); when(pendingFundService.findAll()) .thenReturn(of(pendingFund)); when(azraelClient.getTransactionStatus(prettify(pendingFund.getTransactionHash()))).thenReturn(TransactionStatus.SUCCEEDED); ReflectionUtils.set(pendingFund, "creationDate", LocalDateTime.now().minusDays(2)); pendingFundCleaner.cleanupPendingFunds(); verify(pendingFundService).removePendingFund(pendingFund); }
@Test public void shouldntRemoveWhenThrownExceptionFromAzrael() { final PendingFund pendingFund = PendingFundMother.aPendingFund(); when(pendingFundService.findAll()) .thenReturn(of(pendingFund)); when(azraelClient.getTransactionStatus(prettify(pendingFund.getTransactionHash()))).thenThrow(new IllegalArgumentException("problemos")); pendingFundCleaner.cleanupPendingFunds(); verify(pendingFundService, times(0)).removePendingFund(any(PendingFund.class)); }