### Question:
UserService extends
    PersonService<E, D> { @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#accountName, 'READ')") @Transactional(readOnly = true) public E findByAccountName(String accountName) { return dao.findByAccountName(accountName); } @SuppressWarnings("unchecked")  UserService(); protected  UserService(Class<E> entityClass); @Override @Autowired @Qualifier("userDao") void setDao(D dao); @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#accountName, 'READ')") @Transactional(readOnly = true) E findByAccountName(String accountName); @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#email, 'READ')") @Transactional(readOnly = true) E findByEmail(String email); E registerUser(E user, HttpServletRequest request); @SuppressWarnings("unchecked") void activateUser(String tokenValue); E persistNewUser(E user, boolean encryptPassword); void updatePassword(E user, String rawPassword); @Transactional(readOnly = true) E getUserBySession(); @PostFilter("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(filterObject, 'READ')") @Transactional(readOnly = true) Set<UserGroup> getGroupsOfUser(Integer userId); PasswordEncoder getPasswordEncoder(); void setPasswordEncoder(PasswordEncoder passwordEncoder); Role getDefaultUserRole(); void setDefaultUserRole(Role defaultUserRole);  }### Answer:
@Test public void findByAccountName_shouldFindAsExpected() { String accountName = "testaccount"; User expectedUser = new User("Test", "User", accountName); when(dao.findByAccountName(accountName)).thenReturn(expectedUser); User actualUser = crudService.findByAccountName(accountName); verify(dao, times(1)).findByAccountName(accountName); verifyNoMoreInteractions(dao); assertEquals(expectedUser, actualUser); }
@Test public void findByAccountName_shouldFindNothing() { String accountName = "nonexistingaccount"; User expectedUser = null; when(dao.findByAccountName(accountName)).thenReturn(expectedUser); User actualUser = crudService.findByAccountName(accountName); verify(dao, times(1)).findByAccountName(accountName); verifyNoMoreInteractions(dao); assertEquals(expectedUser, actualUser); }
@Test(expected = HibernateException.class) public void findByAccountName_shouldThrowHibernateException() { String accountName = "erroraccount"; doThrow(new HibernateException("errormsg")) .when(dao).findByAccountName(accountName); crudService.findByAccountName(accountName); verify(dao, times(1)).findByAccountName(accountName); verifyNoMoreInteractions(dao); }