here are examples of a methods :
public <T> T save(final T o){
return (T) sessionFactory.getCurrentSession().save(o);
}
public <T> T get(final Class<T> type, final Long id){
return (T) sessionFactory.getCurrentSession().get(type, id);
}
public <T> List<T> getFieldLike(final Class<T> type, final String propertyName,
final String value, final MatchMode matchMode) {
final Session session = sessionFactory.getCurrentSession();
final Criteria crit = session.createCriteria(type);
crit.add(Restrictions.like(propertyName, value, matchMode));
return crit.list();
}
Any tips on either unit or integration test these ? Pass in a mock session ?
About the only thing you could do in a unit test is mock Session and Criteria and set expectations – I’ve done this for a few cases using JMock and ended up having to write a Hamcrest matcher for the Restricitons. I’m not convinced there’s much value in it, other than blindly increasing the test coverage.
On the other hand – writing an integration test here would be of definite use, set up an in memory database with some data and assert that the methods return the correct objects