Source:
public void setup() { _ticker = mock(Ticker.class); _hostDiscovery = mock(HostDiscovery.class); when(_hostDiscovery.getHosts()).thenReturn(ImmutableList.of(FOO_ENDPOINT, BAR_ENDPOINT, BAZ_ENDPOINT)); _loadBalanceAlgorithm = mock(LoadBalanceAlgorithm.class); when(_loadBalanceAlgorithm.choose(any(Iterable.class))).thenAnswer(new Answer<ServiceEndPoint>() { @Override public ServiceEndPoint answer(InvocationOnMock invocation) throws Throwable { Iterable<ServiceEndPoint> endpoints = (Iterable<ServiceEndPoint>) invocation.getArguments()[0]; return endpoints.iterator().next(); } }); _serviceFactory = (ServiceFactory<Service>) mock(ServiceFactory.class); when(_serviceFactory.create(FOO_ENDPOINT)).thenReturn(FOO_SERVICE); when(_serviceFactory.create(BAR_ENDPOINT)).thenReturn(BAR_SERVICE); when(_serviceFactory.create(BAZ_ENDPOINT)).thenReturn(BAZ_SERVICE); when(_serviceFactory.getLoadBalanceAlgorithm(any(ServicePoolStatistics.class))) .thenReturn(_loadBalanceAlgorithm); _healthCheckExecutor = mock(ScheduledExecutorService.class); when(_healthCheckExecutor.submit(any(Runnable.class))).then(new Answer<Future<?>>() { @Override public Future<?> answer(InvocationOnMock invocation) throws Throwable { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return Futures.immediateFuture(null); } }); _healthCheckScheduledFuture = mock(ScheduledFuture.class); when(_healthCheckExecutor.scheduleAtFixedRate((Runnable) any(), anyLong(), anyLong(), (TimeUnit) any())).then( new Answer<ScheduledFuture<?>>() { @Override public ScheduledFuture<?> answer(InvocationOnMock invocation) throws Throwable { return _healthCheckScheduledFuture; } } ); _pool = new ServicePool<Service>(Service.class, _ticker, _hostDiscovery, _serviceFactory, ServiceCachingPolicyBuilder.NO_CACHING, _healthCheckExecutor, true ); }

Target:
public void setup() { _ticker = mock(Ticker.class); _hostDiscovery = mock(HostDiscovery.class); when(_hostDiscovery.getHosts()).thenReturn(ImmutableList.of(FOO_ENDPOINT, BAR_ENDPOINT, BAZ_ENDPOINT)); _loadBalanceAlgorithm = mock(LoadBalanceAlgorithm.class); when(_loadBalanceAlgorithm.choose(any(Iterable.class))).thenAnswer(new Answer<ServiceEndPoint>() { @Override public ServiceEndPoint answer(InvocationOnMock invocation) throws Throwable { Iterable<ServiceEndPoint> endpoints = (Iterable<ServiceEndPoint>) invocation.getArguments()[0]; return endpoints.iterator().next(); } }); ArgumentCaptor<ServicePoolStatistics> statsCaptor = (ArgumentCaptor) ArgumentCaptor.forClass(ServicePoolStatistics.class); _serviceFactory = (ServiceFactory<Service>) mock(ServiceFactory.class); when(_serviceFactory.create(FOO_ENDPOINT)).thenReturn(FOO_SERVICE); when(_serviceFactory.create(BAR_ENDPOINT)).thenReturn(BAR_SERVICE); when(_serviceFactory.create(BAZ_ENDPOINT)).thenReturn(BAZ_SERVICE); when(_serviceFactory.getLoadBalanceAlgorithm(statsCaptor.capture())) .thenReturn(_loadBalanceAlgorithm); _healthCheckExecutor = mock(ScheduledExecutorService.class); when(_healthCheckExecutor.submit(any(Runnable.class))).then(new Answer<Future<?>>() { @Override public Future<?> answer(InvocationOnMock invocation) throws Throwable { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return Futures.immediateFuture(null); } }); _healthCheckScheduledFuture = mock(ScheduledFuture.class); when(_healthCheckExecutor.scheduleAtFixedRate((Runnable) any(), anyLong(), anyLong(), (TimeUnit) any())).then( new Answer<ScheduledFuture<?>>() { @Override public ScheduledFuture<?> answer(InvocationOnMock invocation) throws Throwable { return _healthCheckScheduledFuture; } } ); _pool = new ServicePool<Service>(Service.class, _ticker, _hostDiscovery, _serviceFactory, UNLIMITED_CACHING, _healthCheckExecutor, true); _servicePoolStatistics = statsCaptor.getValue(); }