### Question:
CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) { return userService.findByUsername(username) .map(this::getUserDetails) .orElseThrow(() -> new UsernameNotFoundException(String.format("Username: %s not found", username))); } CustomUserDetailsService(UserService userService); @Override UserDetails loadUserByUsername(String username);  }### Answer:
@Test public void exceptionMessageShouldContainUsername() { try { userDetailsService.loadUserByUsername("user1"); } catch (UsernameNotFoundException e) { assertEquals("Username: user1 not found", e.getMessage()); } }
@Test public void shouldLUserByUsername() { UserCredentials credentials = new UserCredentials("user", "start", "ROLE_ADMIN"); User user = new User(credentials); given(userService.findByUsername("user")) .willReturn(Optional.of(user)); UserDetails userDetails = userDetailsService.loadUserByUsername("user"); assertEquals("user", userDetails.getUsername()); assertEquals(1, userDetails.getAuthorities().size()); assertTrue(userDetails.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ADMIN"))); }
@Test(expected = UsernameNotFoundException.class) public void shouldThrowExceptionWhenLoadingNonExistingUser() { userDetailsService.loadUserByUsername("user"); }