SpringSecurityAuthenticationService implements AuthenticationService { @Override public UserDetailsAdapter getCurrentUserDetails() throws UserNotLoggedInException { if (SecurityContextHolder.getContext().getAuthentication() != null) { UserDetailsAdapter userDetailsAdapter = (UserDetailsAdapter) SecurityContextHolder.getContext().getAuthentication() .getPrincipal(); if (userDetailsAdapter != null) { return userDetailsAdapter; } } throw new UserNotLoggedInException(); }  @Override UserDetailsAdapter getCurrentUserDetails(); @Override boolean isCurrentUserAuthenticated();  }
@Test public void getCurrentUserDetailsShouldReturnUserDetailsWhenUserIsLoggedIn() throws UserNotLoggedInException { UserDetailsAdapter userDetailsAdapter = new UserDetailsAdapter(USER_ID, USER_PASSWORD, USER_NAME); SecurityContextHolder.getContext() .setAuthentication(new UsernamePasswordAuthenticationToken(userDetailsAdapter, null)); UserDetailsAdapter result = springSecurityAuthenticationService.getCurrentUserDetails(); assertThat(result, is(userDetailsAdapter)); }
@Test(expected = UserNotLoggedInException.class) public void getCurrentUserDetailsShouldNotReturnUserDetailsWhenUserIsNotLoggedIn() throws UserNotLoggedInException { SecurityContextHolder.getContext().setAuthentication(null); UserDetailsAdapter result = springSecurityAuthenticationService.getCurrentUserDetails(); }