开发者

how to easymock test this code

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜