CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(final String email) throws UsernameNotFoundException { final User user = userRepository.findByEmailIgnoreCase(email); if (user == null) { throw new UsernameNotFoundException("User not found."); } return new CustomUserDetails(user); } CustomUserDetailsService(final UserRepository userRepository); @Override UserDetails loadUserByUsername(final String email);  }
@Test(expected = UsernameNotFoundException.class) public void smokeTest_invalidUser() { final String email = "Does-not-exist@example.com"; customUserDetailsService.loadUserByUsername(email); }
@Test public void smokeTest_loadValidUser_sameCase() { final String userEmail = "test" + System.currentTimeMillis() + "@example.com"; final User user = userTestTools.createUser(); user.setEmail(userEmail); userTestTools.save(user); final UserDetails userDetails = customUserDetailsService.loadUserByUsername(userEmail); assertNotNull("Result should be non-null", userDetails); assertTrue("Should be a CustomUserDetails instance", userDetails instanceof CustomUserDetails); assertEquals("Should have correct email", userEmail, userDetails.getUsername()); assertEquals("Should have correct id", user.getId(), ((CustomUserDetails) userDetails).getUserId()); assertNotNull("Should have a user model", ((CustomUserDetails) userDetails).getUserModel()); }
@Test public void smokeTest_loadValidUser_differentCasing() { final String userEmail = "test" + System.currentTimeMillis() + "@example.com"; final User user = userTestTools.createUser(); user.setEmail(userEmail); userTestTools.save(user); final String lookupEmail = userEmail.toUpperCase(); final UserDetails userDetails = customUserDetailsService.loadUserByUsername(lookupEmail); assertNotNull("Result should be non-null", userDetails); assertTrue("Should be a CustomUserDetails instance", userDetails instanceof CustomUserDetails); assertEquals("Should have correct email", userEmail, userDetails.getUsername()); assertEquals("Should have correct id", user.getId(), ((CustomUserDetails) userDetails).getUserId()); assertNotNull("Should have a user model", ((CustomUserDetails) userDetails).getUserModel()); }