Hi i need some help, how to unit test a DAO?
here my code:
@Transactional
public class ProjectDaoImpl extends HibernateDaoSupport implements ProjectDao {
Log logger = LogFactory.getLog(this.getClass());
private String message;
/**
* {@inheritDoc}
*/
public List<User> retrieveAll() {
return this.getHibernateTemplate().loadAll(User.class);
}
/**
* {@inheritDoc}
*/
public boolean saveOrUpdateUser(User user) {
boolean res = false;
try {
this.getHibernateTemplate().saveOrUpdate(user);
res = true;
} catch (DataAccessException e) {
res = false;
this.logger.error("DataAccessException", e);
}
return res;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public User retrieveUserById(Long id) {
User user = null;
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
Criterion criterion = Restrictions.eq("user_id", id);
criteria.add(criterion);
try {
List<User> users = this.getHibernateTemplate().findByCriteria(criteria);
if (users != null && users.size() > 0) user = users.get(0);
} catch (DataAccessException e) {
this.logger.error("DataAccessException", e);
}
return user;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<User> retrieveUserByCriteria(DetachedCriteria criteria) {
List<User> users = null;
try {
users = this.getHibernateTemplate().findByCriteria(criteria);
} catch (DataAccessException e) {
this.logger.error("DataAccessException", e);
}
return users;
}
/**
* {@inheritDoc}
*/
public boolean deleteUser(User user) {
boolean res = false;
try {
this.getHibernateTemplate().delete(user);
res = true;
} catch (DataAccessException e) {
this.logger.error("DataAccessException", e);
}
return res;
}
}