ServletSecurityAuthenticationService extends GroupAdapterAuthorizationSource implements AuthenticationService { @Override public void logout() { HttpServletRequest request = getRequestForThread(); try { request.logout(); } catch (Exception e) { } HttpSession session = request.getSession(false); if (session != null) { try { session.invalidate(); } catch (IllegalStateException ise) { String exceptionMessage = ise.getMessage(); if (exceptionMessage == null || !exceptionMessage.contains("UT000021")) { throw ise; } } } } ServletSecurityAuthenticationService(); @Override User login(String username,
                      String password); @Override boolean isLoggedIn(); @Override void logout(); @Override User getUser();  }
@Test public void testLogout() throws Exception { tested.logout(); verify(request, times(1)).logout(); verify(httpSession, times(1)).invalidate(); }
@Test public void testSwallowIllegalStateExceptionDuringLogoutWithKeycloak() { doThrow(new IllegalStateException("UT000021: Session already invalidated")).when(httpSession).invalidate(); tested.logout(); }
@Test public void testReThrowUnexpectedIllegalStateExceptionDuringLogout() { String exceptionMsg = "This exception should be propagated!"; doThrow(new IllegalStateException(exceptionMsg)).when(httpSession).invalidate(); try { tested.logout(); } catch (IllegalStateException ise) { assertEquals(exceptionMsg, ise.getMessage()); } }