### Question:
DefaultOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); HttpUriRequest request = new HttpGet(tokenKeysEndpointUri); try (CloseableHttpResponse response = httpClient.execute(request)) { String bodyAsString = HttpClientUtil.extractResponseBodyAsString(response); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { return bodyAsString; } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(statusCode) .withResponseBody(bodyAsString) .build(); } } catch (IOException e) { throw new OAuth2ServiceException("Error retrieving token keys: " + e.getMessage()); } } DefaultOAuth2TokenKeyService();  DefaultOAuth2TokenKeyService(@Nonnull CloseableHttpClient httpClient); @Override String retrieveTokenKeys(URI tokenKeysEndpointUri);  }### Answer:
@Test public void retrieveTokenKeys_responseNotOk_throwsException() throws IOException { String errorDescription = "Something wen't wrong"; CloseableHttpResponse response = HttpClientTestFactory .createHttpResponse(errorDescription, HttpStatus.SC_BAD_REQUEST); when(httpClient.execute(any())).thenReturn(response); assertThatThrownBy(() -> cut.retrieveTokenKeys(TOKEN_ENDPOINT_URI)) .isInstanceOf(OAuth2ServiceException.class) .hasMessageContaining(errorDescription); }
@Test public void retrieveTokenKeys_tokenEndpointUriIsNull_throwsException() { assertThatThrownBy(() -> cut.retrieveTokenKeys(null)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void retrieveTokenKeys_errorOccurs_throwsServiceException() throws IOException { String errorMessage = "useful error message"; when(httpClient.execute(any())).thenThrow(new IOException(errorMessage)); assertThatThrownBy(() -> cut.retrieveTokenKeys(TOKEN_ENDPOINT_URI)) .isInstanceOf(OAuth2ServiceException.class) .hasMessageContaining(errorMessage); }
@Test public void retrieveTokenKeys_executesHttpGetRequestWithCorrectURI() throws IOException { mockResponse(); cut.retrieveTokenKeys(TOKEN_ENDPOINT_URI); Mockito.verify(httpClient, times(1)).execute(argThat(isHttpGetAndContainsCorrectURI())); }