### Question:
RbacUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(final String username) { final UserDetails user = userDetailsService.loadUserByUsername(username); final Set<String> roles = AuthorityUtils.authorityListToSet(user.getAuthorities()); final Set<GrantedAuthority> privileges = roleMapper.mapRolesToGrantedAuthority(roles); return new RbacUserDetails(user, privileges); } RbacUserDetailsService(final RoleMapper roleMapper); @Override UserDetails loadUserByUsername(final String username); UserDetailsService getUserDetailsService(); void setUserDetailService(final UserDetailsService userDetailsService); RoleMapper getRoleMapper();  }### Answer:
@Test(expected = UsernameNotFoundException.class) public void testMissingUser() { userDetailsService.loadUserByUsername("userDoesNotExist"); fail(); }
@Test public void testLoadUserWithPrivileges() { final UserDetails userDetails = userDetailsService.loadUserByUsername(userWithPrivileges.getUsername()); verifyCommonFields(userDetails, userWithPrivileges); assertThat(userDetails.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_PRIVILEGE")), is(true)); }
@Test public void testLoadUserWithPrivilegesAndNoSpringRoles() { roleMapper.setSpringRoles(false); final UserDetails userDetails = userDetailsService.loadUserByUsername(userWithPrivileges.getUsername()); verifyCommonFields(userDetails, userWithPrivileges); assertThat(userDetails.getAuthorities().contains(new SimpleGrantedAuthority("PRIVILEGE")), is(true)); }
@Test public void testLoadUserWithoutPrivileges() { final UserDetails userDetails = userDetailsService.loadUserByUsername(userWithoutPrivileges.getUsername()); verifyCommonFields(userDetails, userWithoutPrivileges); assertThat(userDetails.getAuthorities().isEmpty(), is(true)); }
@Test public void testLoadUserWithoutMapping() { final UserDetails userDetails = userDetailsService.loadUserByUsername(userWithoutMapping.getUsername()); verifyCommonFields(userDetails, userWithoutMapping); assertThat(userDetails.getAuthorities().isEmpty(), is(true)); }
@Test public void testLoadUserWithMultipleRoles() { final UserDetails userDetails = userDetailsService.loadUserByUsername(userWithMultipleRoles.getUsername()); verifyCommonFields(userDetails, userWithMultipleRoles); assertThat(userDetails.getAuthorities().size(), is(3)); assertThat(userDetails.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_PRIVILEGE1")), is(true)); assertThat(userDetails.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_PRIVILEGE2")), is(true)); assertThat(userDetails.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_PRIVILEGE3")), is(true)); }