@Test
public void testRefreshAfterFailure() throws Exception {

  underTest = new OAuthCredentialsCache(executorService, mockCredentials);
  final AccessToken accessToken =
      new AccessToken("hi", new Date(HeaderCacheElement.TOKEN_STALENESS_MS + 1));
  //noinspection unchecked
  Mockito.when(mockCredentials.refreshAccessToken())
      // First call will throw Exception & bypass retries
      .thenThrow(new IOException())
      // Second call will succeed
      .thenReturn(accessToken);
  // First call
  OAuthCredentialsCache.HeaderToken token1 =
      underTest.getHeader(TIMEOUT_SECONDS, TimeUnit.SECONDS);
  Assert.assertFalse(token1.getStatus().isOk());
  // Now the second token should be available
  OAuthCredentialsCache.HeaderToken token2 =
      underTest.getHeader(TIMEOUT_SECONDS, TimeUnit.SECONDS);
  Assert.assertTrue(token2.getStatus().isOk());
  Assert.assertThat(token2.getHeader(), containsString("hi"));
  // Make sure that the token was only requested twice: once for the first failure & second time
  // for background recovery
  Mockito.verify(mockCredentials, times(2)).refreshAccessToken();
}