### Question:
AccessTokenRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { try { Map<String, Collection<String>> headers = new HashMap<>(template.headers()); Collection<String> authHeaders = headers.get("Authorization"); if(authHeaders == null) { authHeaders = new ArrayList<>(); } authHeaders.add("Bearer " + this.tokenProvider.getAccessToken()); headers.put("Authorization", authHeaders); template.headers(headers); } catch (Exception e) { throw new UnauthorizedException("Could not retrieve access token from token provider.", e); } } AccessTokenRequestInterceptor(TokenProvider tokenProvider); @Override void apply(RequestTemplate template);  }### Answer:
@Test public void testApply_whenTokenProviderReturnsAccessToken_shouldAddAuthorizationHeaderAndKeepExistingOnes() { requestTemplate.header("Authorization", "EXISTING"); Mockito.when(tokenProvider.getAccessToken()).thenReturn("access-token"); requestInterceptor.apply(requestTemplate); assertThat(requestTemplate.headers()).hasSize(1); Collection<String> authorizationHeader = requestTemplate.headers().get("Authorization"); assertThat(authorizationHeader).isNotNull(); Iterator<String> iterator = authorizationHeader.iterator(); iterator.next(); assertThat(iterator.next()).isEqualTo("Bearer access-token"); }
@Test(expected = UnauthorizedException.class) public void testApply_whenTokenProviderThrowsException_shouldWrapIntoUnauthorizedException() { Mockito.when(tokenProvider.getAccessToken()).thenThrow(new RuntimeException()); requestInterceptor.apply(requestTemplate); }