@Test
public void channelRefreshShouldSwapChannels() throws IOException {

  ManagedChannel underlyingChannel1 = Mockito.mock(ManagedChannel.class);
  ManagedChannel underlyingChannel2 = Mockito.mock(ManagedChannel.class);
  // mock executor service to capture the runnable scheduled so we can invoke it when we want to
  ScheduledExecutorService scheduledExecutorService =
      Mockito.mock(ScheduledExecutorService.class);
  final List<Runnable> channelRefreshers = new ArrayList<>();
  Answer extractChannelRefresher =
      new Answer() {
        public Object answer(InvocationOnMock invocation) {
          channelRefreshers.add((Runnable) invocation.getArgument(0));
          return null;
        }
      };
  Mockito.doAnswer(extractChannelRefresher)
      .when(scheduledExecutorService)
      .schedule(
          Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS));
  FakeChannelFactory channelFactory =
      new FakeChannelFactory(Arrays.asList(underlyingChannel1, underlyingChannel2));
  ManagedChannel refreshingManagedChannel =
      new RefreshingManagedChannel(channelFactory, scheduledExecutorService);
  refreshingManagedChannel.newCall(
      FakeMethodDescriptor.<String, Integer>create(), CallOptions.DEFAULT);
  Mockito.verify(underlyingChannel1, Mockito.only())
      .newCall(Mockito.<MethodDescriptor<String, Integer>>any(), Mockito.any(CallOptions.class));
  // swap channel
  channelRefreshers.get(0).run();
  refreshingManagedChannel.newCall(
      FakeMethodDescriptor.<String, Integer>create(), CallOptions.DEFAULT);
  Mockito.verify(underlyingChannel2, Mockito.only())
      .newCall(Mockito.<MethodDescriptor<String, Integer>>any(), Mockito.any(CallOptions.class));
}