@Test
  public void testListenerErrorCanRestartReplication()
      throws URISyntaxException, InterruptedException {
    Listener listener = new Listener();

    URI uri = new URI("http://127.0.0.1:5984/db1");
    DatastoreExtended mockDatastore = mock(DatastoreExtended.class);
    ReplicationStrategy mockStrategy = mock(ReplicationStrategy.class);
    when(mockStrategy.isReplicationTerminated()).thenReturn(true);
    when(mockStrategy.getEventBus()).thenReturn(new EventBus());

    PullReplication pull = createPullReplication(uri, mockDatastore);
    TestReplicator replicator = new TestReplicator(pull, mockStrategy);
    replicator.getEventBus().register(listener);
    replicator.start();

    // Waits for callbacks to be run
    replicator.error(
        new ReplicationStrategyErrored(
            mockStrategy, new ErrorInfo(new RuntimeException("Mocked error"))));

    Assert.assertTrue(listener.run);
    Assert.assertFalse(listener.errorThrown);

    // Don't leave threads about the place
    replicator.complete(new ReplicationStrategyCompleted(mockStrategy));
  }
  private void startAndVerify() throws Exception {
    Assert.assertEquals(Replicator.State.PENDING, replicator.getState());
    Assert.assertNull(replicator.strategyThread());

    replicator.getEventBus().register(mockListener);
    replicator.start();

    Assert.assertNotNull(replicator.strategyThread());
    Assert.assertEquals(Replicator.State.STARTED, replicator.getState());

    // Make sure the strategy returned before move on, other wise
    // the mock strategy might not be called when you try to verify
    replicator.await();
    Assert.assertFalse(replicator.strategyThread().isAlive());

    verify(mockStrategy).run();

    // No interaction to listener yet
    verifyZeroInteractions(mockListener);
  }