### Question:
CustomUserService extends DefaultUserService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { logger.info("Custom User Service called to get user information"); final User user = userRepository.getByUsername(username); if (user == null) { throw new UsernameNotFoundException("User with username '" + username + "' was not found!"); } fetchCustomCredential(user); return user; } @Autowired  CustomUserService(UserRepository userRepository,
                             PageRepository pageRepository,
                             WidgetRepository widgetRepository,
                             PageTemplateRepository pageTemplateRepository,
                             CategoryRepository categoryRepository,
                             PersonRepository personRepository); @Override UserDetails loadUserByUsername(String username); @Override User getAuthenticatedUser();  }### Answer:
@Test public void testLoadUserByUsername() throws Exception { final String username = "canonical"; User user = new UserImpl("1", username); expect(repository.getByUsername(username)).andReturn(user); replay(repository); UserDetails returnedUser = userService.loadUserByUsername(username); verify(repository); assertEquals(returnedUser, user); }
@Test(expected = UsernameNotFoundException.class) public void testLoadUserByUsername_fail() throws Exception { final String username = "missing"; expect(repository.getByUsername(username)).andReturn(null); expectLastCall(); replay(repository); userService.loadUserByUsername(username); assertFalse("Method should throw Exception", true); }