SingleJVMJobProgressListener implements JobProgressListener { @Override public void afterWrite(List items) { currentItemCount.addAndGet(items.size()); int percentageComplete = currentItemCount.intValue() * 100 / totalItemCount; sentUpdateIfNeeded(percentageComplete); }  @Override @BeforeStep void beforeStep(StepExecution stepExecution); @Override void afterWrite(List items); @Override @AfterStep ExitStatus afterStep(StepExecution stepExecution); @Override void beforeWrite(List items); @Override void onWriteError(Exception exception, List items);  }
@Test public void givenAStepStartedWith1000Employees_whenWritingOneItem_thenNoNewEventIsTriggered() { startAStepWithEmployees(1000); jobProgressListener.afterWrite(aListWithSize(1)); Mockito.verify(eventBus, times(1)).post(eventArgumentCaptor.capture()); JobProgressEvent jobProgressEvent = eventArgumentCaptor.getValue(); checkPercentageComplete(jobProgressEvent, 0); }
@Test public void givenAStepStartedWith100Employees_whenWritingOneItem_thenAProgressEventIsTriggered() { startAStepWithEmployees(100); jobProgressListener.afterWrite(aListWithSize(1)); Mockito.verify(eventBus, times(2)).post(eventArgumentCaptor.capture()); JobProgressEvent jobProgressEvent = eventArgumentCaptor.getValue(); checkPercentageComplete(jobProgressEvent, 1); }
@Test public void givenAStepStartedWith1000Employees_when999ItemsAreWrittenTwice_thenProgressIs99() { startAStepWithEmployees(1000); jobProgressListener.afterWrite(aListWithSize(999)); Mockito.verify(eventBus, times(2)).post(eventArgumentCaptor.capture()); JobProgressEvent jobProgressEvent = eventArgumentCaptor.getValue(); checkPercentageComplete(jobProgressEvent, 99); }
@Test public void givenAStepStarted_whenWritingAllItemsAtOnce_thenAnEventWith100ProgressIsTriggered() { startAStepWithEmployees(100); jobProgressListener.afterWrite(aListWithSize(100)); Mockito.verify(eventBus, times(2)).post(eventArgumentCaptor.capture()); checkPercentageComplete(eventArgumentCaptor.getAllValues().get(0), 0); checkPercentageComplete(eventArgumentCaptor.getAllValues().get(1), 100); }
@Test public void givenAStepStartedWith100Employees_when5ItemsAreWritten_thenAnEventIsTriggeredWithProgress() { startAStepWithEmployees(100); jobProgressListener.afterWrite(aListWithSize(25)); jobProgressListener.afterWrite(aListWithSize(25)); jobProgressListener.afterWrite(aListWithSize(25)); jobProgressListener.afterWrite(aListWithSize(25)); Mockito.verify(eventBus, times(5)).post(eventArgumentCaptor.capture()); checkPercentageComplete(eventArgumentCaptor.getAllValues().get(0), 0); checkPercentageComplete(eventArgumentCaptor.getAllValues().get(1), 25); checkPercentageComplete(eventArgumentCaptor.getAllValues().get(2), 50); checkPercentageComplete(eventArgumentCaptor.getAllValues().get(3), 75); checkPercentageComplete(eventArgumentCaptor.getAllValues().get(4), 100); }