@Test public void testStart() throws Exception { TestPayScheduler scheduler = new TestPayScheduler(); final Random random = new Random(); PendingCashRepository cashRepository = mock(PendingCashRepository.class); PayCash cash = mock(PayCash.class); when(cashRepository.findPending(any(Date.class), anyInt(), anyInt())) .thenReturn(ImmutableList.of(cash, cash, cash, cash)); when(cash.isApproved()).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { int rand = random.nextInt(2); switch (rand) { case 0: APPROVED_STATUS_COUNT++; return true; default: return false; } } }); when(cash.getStatus()).thenAnswer(new Answer<CashStatus>() { @Override public CashStatus answer(InvocationOnMock invocation) throws Throwable { int rand = random.nextInt(2); switch (rand) { case 0: return CashStatus.CANCELLED; default: CREATED_STATUS_COUNT++; return CashStatus.CREATED; } } }); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { CONFIRMED_COUNT++; return null; } }).when(cash).confirm(eq("paid123"), any(Date.class)); Repository<PaidFee> paidFeeRepository = mock(Repository.class); PaidFee fee = mock(PaidFee.class); when(fee.isOffseted()).thenReturn(true); when(fee.isPending()).thenReturn(true); when(paidFeeRepository.load(anyString())).thenReturn(fee); scheduler.setPendingCashRepository(cashRepository); scheduler.setPaidFeeRepository(paidFeeRepository); scheduler.start(); TimeUnit.SECONDS.sleep(5); scheduler.stop(); assertThat(CONFIRMED_COUNT, lessThan(APPROVED_STATUS_COUNT)); assertThat(CONFIRMED_COUNT, lessThan(CREATED_STATUS_COUNT)); verify(cashRepository, times(CONFIRMED_COUNT)).save(any(Cash.class)); verify(fee, times(CONFIRMED_COUNT)).confirm(eq("paid123"), any(Date.class)); }
public void start() { scheduler.scheduleWithFixedDelay( new Worker(pendingCashRepository), initialDelay, period, TimeUnit.MILLISECONDS); }
PayScheduler { public void start() { scheduler.scheduleWithFixedDelay( new Worker(pendingCashRepository), initialDelay, period, TimeUnit.MILLISECONDS); } }
PayScheduler { public void start() { scheduler.scheduleWithFixedDelay( new Worker(pendingCashRepository), initialDelay, period, TimeUnit.MILLISECONDS); } PayScheduler(); }
PayScheduler { public void start() { scheduler.scheduleWithFixedDelay( new Worker(pendingCashRepository), initialDelay, period, TimeUnit.MILLISECONDS); } PayScheduler(); void start(); void stop(); void setPendingCashRepository(PendingCashRepository<T> pendingRepository); void setPaidFeeRepository(Repository<PaidFee> paidFeeRepository); }
PayScheduler { public void start() { scheduler.scheduleWithFixedDelay( new Worker(pendingCashRepository), initialDelay, period, TimeUnit.MILLISECONDS); } PayScheduler(); void start(); void stop(); void setPendingCashRepository(PendingCashRepository<T> pendingRepository); void setPaidFeeRepository(Repository<PaidFee> paidFeeRepository);  }