### Question:
LibraryUserDetailsService implements UserDetailsService, UserDetailsPasswordService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { return userService .findOneByEmail(username) .map(AuthenticatedUser::new) .orElseThrow(() -> new UsernameNotFoundException("No user found for " + username)); } LibraryUserDetailsService(UserService userService); @Override UserDetails loadUserByUsername(String username); @Override UserDetails updatePassword(UserDetails user, String newPassword);  }### Answer:
@DisplayName("can load existing user") @Test void loadUserByUsername() { given(userService.findOneByEmail(any())) .willReturn( Optional.of( new User( "Hans", "Test", "test@example.com", "secret", Collections.singleton("USER")))); UserDetails userDetails = cut.loadUserByUsername("test@example.com"); assertThat(userDetails).isNotNull().isInstanceOf(AuthenticatedUser.class); assertThat(userDetails.getAuthorities().iterator().next().getAuthority()).isEqualTo("ROLE_USER"); }
@DisplayName("reports expected error when user does not exist") @Test void loadUserByUsernameNotFound() { given(userService.findOneByEmail(any())).willReturn(Optional.empty()); assertThatExceptionOfType(UsernameNotFoundException.class) .isThrownBy(() -> cut.loadUserByUsername("test@example.com")) .withMessage("No user found for test@example.com") .withNoCause(); }
@DisplayName("can load existing user") @Test void loadUserByUsername() { given(userService.findOneByEmail(any())) .willReturn( Optional.of( new User( "Hans", "Test", "test@example.com", "secret", Collections.singleton("USER")))); UserDetails userDetails = cut.loadUserByUsername("test@example.com"); assertThat(userDetails).isNotNull().isInstanceOf(AuthenticatedUser.class); }