### Question:
SpringOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); try { ResponseEntity<String> response = restOperations.getForEntity(tokenKeysEndpointUri, String.class); if (HttpStatus.OK.value() == response.getStatusCode().value()) { return response.getBody(); } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(response.getStatusCodeValue()) .withResponseBody(response.getBody()) .build(); } } catch (HttpClientErrorException ex) { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(ex.getStatusCode().value()) .withResponseBody(ex.getResponseBodyAsString()) .build(); } catch (Exception e) { throw OAuth2ServiceException.builder("Unexpected error retrieving token keys: " + e.getMessage()) .withUri(tokenKeysEndpointUri) .build(); } } SpringOAuth2TokenKeyService(@Nonnull RestOperations restOperations); @Override String retrieveTokenKeys(URI tokenKeysEndpointUri);  }### Answer:
@Test public void retrieveTokenKeys_endpointUriIsNull_throwsException() throws OAuth2ServiceException { assertThatThrownBy(() -> cut.retrieveTokenKeys(null)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void retrieveTokenKeys_usesGivenURI() throws OAuth2ServiceException { mockResponse(); cut.retrieveTokenKeys(TOKEN_KEYS_ENDPOINT_URI); Mockito.verify(restOperationsMock, times(1)) .getForEntity(TOKEN_KEYS_ENDPOINT_URI, String.class); }
@Test public void retrieveTokenKeys_badResponse_throwsException() { String errorMessage = "useful error message"; mockResponse(errorMessage, HttpStatus.BAD_REQUEST); assertThatThrownBy(() -> cut.retrieveTokenKeys(TOKEN_KEYS_ENDPOINT_URI)) .isInstanceOf(OAuth2ServiceException.class) .hasMessageContaining(TOKEN_KEYS_ENDPOINT_URI.toString()) .hasMessageContaining(String.valueOf(HttpStatus.BAD_REQUEST.value())) .hasMessageContaining(errorMessage); }