### Question:
RoleService extends
    PermissionAwareCrudService<E, D> { @Transactional(readOnly = true) public E findByRoleName(String roleName) { SimpleExpression eqRoleName = Restrictions.eq("name", roleName); E role = dao.findByUniqueCriteria(eqRoleName); return role; } @SuppressWarnings("unchecked")  RoleService(); protected  RoleService(Class<E> entityClass); @Transactional(readOnly = true) E findByRoleName(String roleName); @Override @Autowired @Qualifier("roleDao") void setDao(D dao);  }### Answer:
@Test public void findByRoleName_shouldFindAsExpected() { String roleName = "ROLE_TEST"; Role expectedRole = new Role(roleName); when(dao.findByUniqueCriteria(any(SimpleExpression.class))).thenReturn(expectedRole); Role actualRole = crudService.findByRoleName(roleName); verify(dao, times(1)).findByUniqueCriteria(any(SimpleExpression.class)); verifyNoMoreInteractions(dao); assertEquals(expectedRole, actualRole); }
@Test public void findByRoleName_shouldFindNothing() { String roleName = "ROLE_THAT_NOT_EXISTS"; Role expectedRole = null; when(dao.findByUniqueCriteria(any(SimpleExpression.class))).thenReturn(expectedRole); Role actualRole = crudService.findByRoleName(roleName); verify(dao, times(1)).findByUniqueCriteria(any(SimpleExpression.class)); verifyNoMoreInteractions(dao); assertEquals(expectedRole, actualRole); }
@Test(expected = HibernateException.class) public void findByRoleName_shouldThrowHibernateException() { String roleName = "ROLE_THAT_THROWS_EXCEPTION"; doThrow(new HibernateException("errormsg")) .when(dao).findByUniqueCriteria(any(SimpleExpression.class)); crudService.findByRoleName(roleName); verify(dao, times(1)).findByUniqueCriteria(any(SimpleExpression.class)); verifyNoMoreInteractions(dao); }