title: Mockito thenAnswer doesn't seem to work as expected
body: <p>I am writing unit test for the following code which uses AsyncHttpClient. The use of CountDownLatch and decrementing of the CountDownLatch within the implementation of FutureCallback is causing my JUnit testcase to hang waiting for countdown latch to be decremented. Within the JUnit test I am capturing the FutureCallback using ArgumentCaptor and then using thenAnswer I am calling the completed method to decrement the count down latch. But it doesn't seems to work, any ideas will be helpful.</p>
  
  <pre><code>public List&lt;QueryResponse&gt; execute(Query query) {
      List&lt;QueryResponse&gt; res = new ArrayList&lt;QueryResponse&gt;();
  
      try {
          List&lt;HttpRequestBase&gt; requests = query.generateHttpRequests();
          List&lt;Future&lt;HttpResponse&gt;&gt; futures = new ArrayList&lt;Future&lt;HttpResponse&gt;&gt;();
  
          final CountDownLatch requestCompletionCDLatch = new CountDownLatch(requests.size());
          for (HttpRequestBase request : requests) {
              futures.add(httpClient.execute(request, new FutureCallback&lt;HttpResponse&gt;() {
  
                  @Override
                  public void failed(Exception e) {
                      logger.error("Error while executing: " + request.toString(), e);
                      requestCompletionCDLatch.countDown();
                  }
  
                  @Override
                  public void completed(HttpResponse result) {
                      requestCompletionCDLatch.countDown();
                  }
  
                  @Override
                  public void cancelled() {
                      logger.info("Request cancelled while executing: " + request.toString());
                      requestCompletionCDLatch.countDown();
                  }
              }));
          }
  
          requestCompletionCDLatch.await();
          for (Future&lt;HttpResponse&gt; future : futures) {
              HttpResponse response = future.get(rcaRequestTimeout, TimeUnit.SECONDS);
              int status = response.getStatusLine().getStatusCode();
              if (status != HttpStatus.SC_OK) {
                  logger.warn("Query with non-success status " + status);
              } else {
                  res.add(query.parseResponse(response.getEntity().getContent()));
              }
          }
      } catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
          logger.error("Error while querying", e);
      } catch (URISyntaxException e) {
          logger.error("Error while generating the query", e);
      }
      return res;
  }
  </code></pre>
  
  <p>My unit test is as follows:</p>
  
  <pre><code>@Test
  public void testHttpError() throws InterruptedException, ExecutionException, TimeoutException {
      StatusLine status = Mockito.mock(StatusLine.class);
      when(status.getStatusCode()).thenReturn(400);
  
      HttpResponse response = Mockito.mock(HttpResponse.class);
      when(response.getStatusLine()).thenReturn(status);
  
      Future&lt;HttpResponse&gt; future = Mockito.mock(Future.class);
      when(future.get(anyLong(), any())).thenReturn(response);
  
      CloseableHttpAsyncClient httpClient = Mockito.mock(CloseableHttpAsyncClient.class);
      ArgumentCaptor&lt;HttpUriRequest&gt; requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
      ArgumentCaptor&lt;FutureCallback&lt;HttpResponse&gt;&gt; futureCallbackCaptor = ArgumentCaptor.forClass((Class)FutureCallback.class);
      when(httpClient.execute(any(), any())).thenReturn(future).thenAnswer(new Answer() {
  
          @Override
          public Object answer(InvocationOnMock invocation) throws Throwable {
              verify(httpClient).execute(requestCaptor.capture(), futureCallbackCaptor.capture());
              futureCallbackCaptor.getValue().completed(response);
              return null;
          }
      });
  
      StubbedRcaClient rcaClient = new StubbedRcaClient(httpClient);
      Query query = new Query("abc", "xyz", RcaHttpRequestType.GET, 1000);
      List&lt;QueryResponse&gt; res = rcaClient.execute(query);
      assertEquals(0, res.size());
      IOUtils.closeQuietly(rcaClient);
  }
  </code></pre>