EasyMock 和 Hibernate 标准查询
我正在尝试使用 JUnit4 和 EasyMock 2.4 测试使用 hibernate criteria API 的 dao 方法。
当我运行测试装置“testGetAsset”时,我收到以下异常:
java.lang.AssertionError:
Unexpected method call add(name=Test):
add(name=Test): expected: 1, actual: 0
add(source=GSFP): expected: 1, actual: 0
uniqueResult(): expected: 1, actual: 0
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:32)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:61)
at $Proxy7.add(Unknown Source)
at com.hsbc.sfd.funddb.persistence.dao.AssetDaoImpl.getAsset(AssetDaoImpl.java:80)
at com.hsbc.sfd.funddb.persistence.AssetDaoTest.testGetAsset(AssetDaoTest.java:62)
我认为问题与未使用 Asset.class 初始化的模拟条件对象有关,但总的来说,我是 EasyMock 和模拟对象的初学者,所以如果有人能看一下并告诉我需要做什么才能通过测试,我将非常感激。
非常感谢, 标记
代码如下:
Dao方法
public Asset getAsset(String name, Source source) {
return (Asset) this.sessionFactory.getCurrentSession()
.createCriteria(Asset.class)
.add(Restrictions.eq("name", name))
.add(Restrictions.eq("source", source))
.uniqueResult();
}
测试类
public class AssetDaoTest {
private SessionFactory factory;
private Session session;
private Criteria criteria;
private AssetDaoImpl dao;
@Before
public void setUp() {
factory = createMock(SessionFactory.class);
session = createMock(Session.class);
criteria = createMock(Criteria.class);
dao = new AssetDaoImpl();
dao.setSessionFactory(factory);
}
@Test
public void testGetAsset() {
String name = "Test";
Source source = Source.GSFP;
Asset asset = new Asset();
asset.setName(name);
asset.setSource(source);
expect(factory.getCurrentSession()).andReturn(session);
expect(session.createCriteria(Asset.class)).andReturn(criteria);
expect(criteria.add(Restrictions.eq("name", name))).andReturn(criteria);
expect(criteria.add(Restrictions.eq("source", source))).andReturn(criteria);
expect(criteria.uniqueResult()).andReturn(asset);
replay(factory, session, criteria);
dao.getAsset(name, source);
}
}
I am trying to test a dao method which uses the hibernate criteria API, using JUnit4 and EasyMock 2.4.
When I run the test fixture 'testGetAsset' I receive the following exception:
java.lang.AssertionError:
Unexpected method call add(name=Test):
add(name=Test): expected: 1, actual: 0
add(source=GSFP): expected: 1, actual: 0
uniqueResult(): expected: 1, actual: 0
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:32)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:61)
at $Proxy7.add(Unknown Source)
at com.hsbc.sfd.funddb.persistence.dao.AssetDaoImpl.getAsset(AssetDaoImpl.java:80)
at com.hsbc.sfd.funddb.persistence.AssetDaoTest.testGetAsset(AssetDaoTest.java:62)
I think the problem is related to the mock criteria object not being initialized with Asset.class, but I'm quite a beginner with EasyMock and mock objects in general, so I'd really appreciate it if someone could take a look and tell me what I need to do to make the test pass.
Many thanks,
Mark
Code is below:
Dao Method
public Asset getAsset(String name, Source source) {
return (Asset) this.sessionFactory.getCurrentSession()
.createCriteria(Asset.class)
.add(Restrictions.eq("name", name))
.add(Restrictions.eq("source", source))
.uniqueResult();
}
Test Class
public class AssetDaoTest {
private SessionFactory factory;
private Session session;
private Criteria criteria;
private AssetDaoImpl dao;
@Before
public void setUp() {
factory = createMock(SessionFactory.class);
session = createMock(Session.class);
criteria = createMock(Criteria.class);
dao = new AssetDaoImpl();
dao.setSessionFactory(factory);
}
@Test
public void testGetAsset() {
String name = "Test";
Source source = Source.GSFP;
Asset asset = new Asset();
asset.setName(name);
asset.setSource(source);
expect(factory.getCurrentSession()).andReturn(session);
expect(session.createCriteria(Asset.class)).andReturn(criteria);
expect(criteria.add(Restrictions.eq("name", name))).andReturn(criteria);
expect(criteria.add(Restrictions.eq("source", source))).andReturn(criteria);
expect(criteria.uniqueResult()).andReturn(asset);
replay(factory, session, criteria);
dao.getAsset(name, source);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,您在模拟设置中传递到 criteria.add 的限制与在 DAO 级别传递的对象实例并不完全相同。
看一下 EasyMock 的参数匹配器。 您可以创建自己的参数匹配器,这可能会为您提供所需的灵活性,但需要做一些工作。
也许值得考虑一下您在这里真正测试的是什么。 使用 Hibernate 测试 DAO 的一个好方法是使用 HSQL 之类的东西来模拟数据库,而不是尝试模拟 Hibernate 标准 API。 然后,您可以将一些示例数据加载到内存数据库中,并确保您的 DAO 运行正常。
The problem is that the Restrictions you're passing into criteria.add in the mock set up are not the exact same object instances that are passed in at the DAO level.
Take a look at EasyMock's argument matchers. You can create your own argument matchers that might give you the flexibility you're looking for, with a bit of work.
It may be worth thinking about what you're really testing here. A good way to test DAOs with hibernate is to mock your database using something like HSQL, instead of trying to mock out the hibernate criteria API. Then you can load some sample data into your in-memory database and just ensure that your DAO is behaving correctly.