### Question:
JestHttpObjectFactory implements ClientObjectFactory<JestClient, Bulk> { protected JestResultHandler<JestResult> createResultHandler(Bulk bulk, Function<Bulk, Boolean> failureHandler) { return new JestResultHandler<JestResult>() { @Override public void completed(JestResult result) { backoffPolicy.deregister(bulk); if (!result.isSucceeded()) { LOG.warn(result.getErrorMessage()); failureHandler.apply(bulk); } } @Override public void failed(Exception ex) { LOG.warn(ex.getMessage(), ex); backoffPolicy.deregister(bulk); failureHandler.apply(bulk); } }; } protected  JestHttpObjectFactory(Builder builder); @Override Collection<String> getServerList(); @Override JestClient createClient(); @Override Function<Bulk, Boolean> createBatchListener(FailoverPolicy failoverPolicy); @Override Function<Bulk, Boolean> createFailureHandler(FailoverPolicy failover); @Override BatchOperations<Bulk> createBatchOperations(); @Override void execute(IndexTemplate indexTemplate); @Override void addOperation(Operation operation); @PluginBuilderFactory static Builder newBuilder(); @Override void start(); @Override void stop(); @Override boolean isStarted(); @Override boolean isStopped();  }### Answer:
@Test public void failoverIsExecutedAfterNonSuccessfulRequest() { Builder builder = createTestObjectFactoryBuilder(); JestHttpObjectFactory config = spy(builder.build()); String payload1 = "test1"; String payload2 = "test2"; Bulk bulk = createTestBatch(payload1, payload2); Function<Bulk, Boolean> failoverHandler = mock(Function.class); JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler); JestResult result = mock(JestResult.class); when(result.isSucceeded()).thenReturn(false); resultHandler.completed(result); ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class); verify(failoverHandler, times(1)).apply(captor.capture()); assertEquals(bulk, captor.getValue()); }
@Test public void failoverIsNotExecutedAfterSuccessfulRequest() { Builder builder = createTestObjectFactoryBuilder(); JestHttpObjectFactory config = spy(builder.build()); String payload1 = "test1"; String payload2 = "test2"; Bulk bulk = createTestBatch(payload1, payload2); Function<Bulk, Boolean> failoverHandler = mock(Function.class); JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler); JestResult result = mock(JestResult.class); when(result.isSucceeded()).thenReturn(true); resultHandler.completed(result); verify(failoverHandler, never()).apply(Mockito.any(Bulk.class)); }
@Test public void failoverIsExecutedAfterFailedRequest() { Builder builder = createTestObjectFactoryBuilder(); JestHttpObjectFactory config = spy(builder.build()); String payload1 = "test1"; String payload2 = "test2"; Bulk bulk = createTestBatch(payload1, payload2); Function<Bulk, Boolean> failoverHandler = mock(Function.class); JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler); resultHandler.failed(new IOException()); ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class); verify(failoverHandler, times(1)).apply(captor.capture()); assertEquals(bulk, captor.getValue()); }