### Question:
SecurityService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { User user = userDao.getByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found: " + username); } if (user.getRole() == null) { user.setRole("ROLE_USER"); } return user; } SecurityService(UserDao userDao); @Override UserDetails loadUserByUsername(String username);  }### Answer:
@Test public void testLoadUserByUsername() throws Exception { when(userDao.getByUsername(username)).thenReturn(user); assertSame(user, securityService.loadUserByUsername(username)); }
@Test(expectedExceptions = UsernameNotFoundException.class) public void testUserNotFound() { when(userDao.getByUsername(username)).thenReturn(null); securityService.loadUserByUsername(username); }