### Question:
BufferedJestHttpObjectFactory extends JestHttpObjectFactory { @Override public Function<Bulk, Boolean> createFailureHandler(FailoverPolicy failover) { return bulk -> { BufferedBulk bufferedBulk = (BufferedBulk)bulk; LOG.warn(String.format("Batch of %s items failed. Redirecting to %s", bufferedBulk.getActions().size(), failover.getClass().getName())); try { bufferedBulk.getActions().stream() .map(item -> failedItemOps.createItem(((BufferedIndex) item))) .forEach(failover::deliver); return true; } catch (Exception e) { LOG.error("Unable to execute failover", e); return false; } }; } protected  BufferedJestHttpObjectFactory(Builder builder); @Override Function<Bulk, Boolean> createFailureHandler(FailoverPolicy failover); @Override BatchOperations<Bulk> createBatchOperations(); @PluginBuilderFactory static Builder newBuilder(); @Override void start(); @Override void stop(); @Override boolean isStarted(); @Override boolean isStopped(); static final String PLUGIN_NAME; }### Answer:
@Test public void failureHandlerExecutesFailoverForEachBatchItemSeparately() { BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder(); ClientObjectFactory<JestClient, Bulk> config = builder.build(); FailoverPolicy failoverPolicy = Mockito.spy(new NoopFailoverPolicy()); String payload1 = "test1"; String payload2 = "test2"; ItemSource<ByteBuf> source1 = createDefaultTestBuffereItemSource(payload1); ItemSource<ByteBuf> source2 = createDefaultTestBuffereItemSource(payload2); Bulk bulk = createTestBatch(source1, source2); config.createFailureHandler(failoverPolicy).apply(bulk); ArgumentCaptor<FailedItemSource> captor = ArgumentCaptor.forClass(FailedItemSource.class); verify(failoverPolicy, times(2)).deliver(captor.capture()); FailedItemSource<ByteBuf> item1 = captor.getAllValues().get(0); assertTrue(item1.getSource().toString(Charset.defaultCharset()).equals(payload1)); FailedItemSource<ByteBuf> item2 = captor.getAllValues().get(1); assertTrue(item2.getSource().toString(Charset.defaultCharset()).equals(payload2)); }