### Question:
InMemoryUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) { final List<AppUser> users = Arrays.asList( new AppUser(1, "user", passwordEncoder.encode("user")), new AppUser(2, "expire", passwordEncoder.encode("expire")) ); return users.stream() .filter(f -> f.getUsername().equals(username)) .map(appUser -> new User(appUser.getUsername(), appUser.getPassword(), AuthorityUtils.NO_AUTHORITIES) ) .findFirst() .orElseThrow(() -> new UsernameNotFoundException("Username: " + username + " not found.")); } @Autowired  InMemoryUserDetailsService(BCryptPasswordEncoder passwordEncoder); @Override UserDetails loadUserByUsername(String username);  }### Answer:
@Test void shouldEncodePasswordAndReturnFoundUsername() { Mockito.when(encoder.encode("user")).thenReturn("hashcodeUser"); UserDetails userDetails = inMemoryUserDetailsService.loadUserByUsername("user"); assertEquals("user", userDetails.getUsername()); assertEquals("hashcodeUser", userDetails.getPassword()); assertTrue(userDetails.isAccountNonExpired()); assertTrue(userDetails.isAccountNonLocked()); assertTrue(userDetails.isCredentialsNonExpired()); assertTrue(userDetails.isEnabled()); }
@Test void shouldThrowExceptionIfUsernameNotFound() { Exception exception = assertThrows(UsernameNotFoundException.class, () -> inMemoryUserDetailsService.loadUserByUsername("andrea"), "Expected exception is not UsernameNotFoundException"); assertEquals("Username: andrea not found.", exception.getMessage()); }