### Question:
UsersTokenService { public TokenDetails retrieveUsersTokenDetails() { TokenDetails tokenDetails = retrieveUsersTokenDetailsFromSecurityContext(); if (tokenDetails.isNotEmpty() && oidcAuthenticationHelper.isTokenExpiredOrShouldBeRefreshed(tokenDetails)) { return retrieveUpdatedUsersTokenDetails(tokenDetails); } return tokenDetails; } @Autowired  UsersTokenService(OidcAuthenticationHelper oidcAuthenticationHelper,
                             OAuthClient oAuthClient); TokenDetails retrieveUsersTokenDetails();  }### Answer:
@Test void retrieveUsersTokenDetailsTokenIsExpired() { SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken("login", tokenDetails, new ArrayList<>())); when(oidcAuthenticationHelper.isTokenExpiredOrShouldBeRefreshed(tokenDetails)).thenReturn(false); assertEquals(service.retrieveUsersTokenDetails(), tokenDetails); }
@Test void retrieveUsersTokenDetailsTokenIsNotExpired() { SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken("login", tokenDetails, new ArrayList<>())); when(oidcAuthenticationHelper.isTokenExpiredOrShouldBeRefreshed(tokenDetails)).thenReturn(true); TokenDetails updatedTokenDetails = new TokenDetails(); when(oAuthClient.obtainTokenDetailsByRefreshToken((tokenDetails.getRefreshToken()))) .thenReturn(updatedTokenDetails); doNothing().when(oidcAuthenticationHelper).authenticateCurrentThread(updatedTokenDetails); assertEquals(service.retrieveUsersTokenDetails(), updatedTokenDetails); }
@Test void retrieveUsersTokenDetailsExceptionWhenTokenRefreshing() { SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken("login", tokenDetails, new ArrayList<>())); when(oidcAuthenticationHelper.isTokenExpiredOrShouldBeRefreshed(tokenDetails)).thenReturn(true); when(oAuthClient.obtainTokenDetailsByRefreshToken(tokenDetails.getRefreshToken())) .thenThrow(new ObtainRefreshTokenException()); assertEquals(service.retrieveUsersTokenDetails(), tokenDetails); }
@Test void retrieveUsersTokenDetailsWhenSecurityContextIsEmpty() { SecurityContextHolder.clearContext(); TokenDetails tokenDetails = service.retrieveUsersTokenDetails(); assertThat(tokenDetails.isEmpty(), is(true)); }