how this code test with easymock
return (Long) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException, SQLException {
Criteria criteria = s.createCriteria(EntityData.class);
criteria.setProjection(Projections.rowCount());
criteria.add(Restrictions.like(param, val));
return ((Long)criteria.uniqueResult()).longValue();
开发者_如何学C }
});
As this is a pure database access operation, I wouldn't mock anything. Just write an unit test, which tests if it returns the correct result and if it's still correct when the data changes.
Therefore you should provide a special datasource for tests, so that your productive system isn't affected. If it's not possible exchange the database configuration, separate your test data by conventions, for example ids starting with 0 are used for tests.
But you could even skip unit testing for this method and write an integration test instead.
精彩评论