### Question:
JWTLogoutHandler implements LogoutHandler { @Override public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { Optional<String> token = authenticationService.getJwtTokenFromRequest(request); try { if (token.isPresent()) { invalidateJwtToken(failure, request, response, token.get()); } else { failure.onAuthenticationFailure(request, response, new TokenNotProvidedException("The token you are trying to logout is not present in the header")); } } catch (ServletException e) { log.error("The response cannot be written during the logout exception handler: {}", e.getMessage()); } }  @Override void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication);  }### Answer:
@Test void givenNoToken_whenLogout_thenHandleFailure() throws ServletException { when(authenticationService.getJwtTokenFromRequest(request)).thenReturn(Optional.empty()); handler.logout(request, response, authentication); verify(failedAuthenticationHandler, times(1)).onAuthenticationFailure(any(), any(), any()); }
@Test void givenAlreadyInvalidatedToken_whenLogout_thenHandleFailure() throws ServletException { when(authenticationService.isInvalidated(TOKEN)).thenReturn(true); handler.logout(request, response, authentication); verify(failedAuthenticationHandler, times(1)).onAuthenticationFailure(any(), any(), any()); }
@Test void givenLogoutRequest_whenFailureHandlingError_thenThrowError() throws ServletException { when(authenticationService.getJwtTokenFromRequest(request)).thenReturn(Optional.empty()); Mockito.doThrow(new ServletException("msg")).when(failedAuthenticationHandler).onAuthenticationFailure(any(), any(), any()); expectedException.expect(ServletException.class); expectedException.expectMessage("The response cannot be written during the logout exception handler: msg"); handler.logout(request, response, authentication); }