### Question:
AbstractUserTokenService extends AbstractTokenService<E, D> { @Transactional(readOnly = true) public E findByUser(User user) { SimpleExpression eqUser = Restrictions.eq("user", user); E userToken = dao.findByUniqueCriteria(eqUser); return userToken; } @SuppressWarnings("unchecked")  AbstractUserTokenService(); protected  AbstractUserTokenService(Class<E> entityClass); @Transactional(readOnly = true) E findByUser(User user); @Override @Transactional(readOnly = true) void validateToken(E userToken);  }### Answer:
@Test public void findByUser_shouldFindUserToken() { User user = new User(); when(dao.findByUniqueCriteria(any(SimpleExpression.class))).thenReturn(implToTest); E actualUserToken = crudService.findByUser(user); verify(dao, times(1)).findByUniqueCriteria(any(SimpleExpression.class)); verifyNoMoreInteractions(dao); assertEquals(actualUserToken, implToTest); }
@Test public void findByUser_shouldFindNothing() { User userWithoutToken = new User(); when(dao.findByUniqueCriteria(any(SimpleExpression.class))).thenReturn(null); E actualUserToken = crudService.findByUser(userWithoutToken); verify(dao, times(1)).findByUniqueCriteria(any(SimpleExpression.class)); verifyNoMoreInteractions(dao); assertNull(actualUserToken); }
@Test(expected = HibernateException.class) public void findByUser_shouldThrowHibernateException() { User userThatThrowsException = new User(); doThrow(new HibernateException("errormsg")) .when(dao).findByUniqueCriteria(any(SimpleExpression.class)); crudService.findByUser(userThatThrowsException); verify(dao, times(1)).findByUniqueCriteria(any(SimpleExpression.class)); verifyNoMoreInteractions(dao); }