Hibernate+GroovyTestcase:无法找出下面代码中的错误..
我在下面的测试中遇到测试失败。具体来说,它抱怨“expect(mockSession.save(hibernateTransitInfo)).andReturn(hibernateTransitInfo)”,并抱怨“不兼容的返回类型”
测试代码
void testCreateTransitFileInfo()
{
HibernateTransitInfo hibernateTransitInfo =
new HibernateTransitInfo(relationshipId: "12345")
expect(mockSessionFactory.currentSession).andReturn(mockSession)
expect(mockSession.save(hibernateTransitInfo)).andReturn(hibernateTransitInfo)
replayAll()
transitFileDao.createHibernateTransitInfo(hibernateTransitInfo)
verifyAll()
}
Actual DaoImplementation
@Repository("transitFileDao")
class TransitFileDaoImpl implements TransitFileDao{
@Autowired
SessionFactory sessionFactory
Session getCurrentSession()
{
return sessionFactory.currentSession
}
void createHibernateTransitInfo(HibernateTransitInfo hibernateTransitInfo)
{
currentSession.save(hibernateTransitInfo)
}
}
I am getting a test failure on the test below. Specifically it complains for "expect(mockSession.save(hibernateTransitInfo)).andReturn(hibernateTransitInfo)" and it complains "incompatible return type"
Test code
void testCreateTransitFileInfo()
{
HibernateTransitInfo hibernateTransitInfo =
new HibernateTransitInfo(relationshipId: "12345")
expect(mockSessionFactory.currentSession).andReturn(mockSession)
expect(mockSession.save(hibernateTransitInfo)).andReturn(hibernateTransitInfo)
replayAll()
transitFileDao.createHibernateTransitInfo(hibernateTransitInfo)
verifyAll()
}
Actual DaoImplementation
@Repository("transitFileDao")
class TransitFileDaoImpl implements TransitFileDao{
@Autowired
SessionFactory sessionFactory
Session getCurrentSession()
{
return sessionFactory.currentSession
}
void createHibernateTransitInfo(HibernateTransitInfo hibernateTransitInfo)
{
currentSession.save(hibernateTransitInfo)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
session.save(X) 的返回类型是一个 Serialized ,表示新创建的 X 的标识符,因此它应该返回 HibernateTransitInfo 的 id,而不是 HibernateTransitInfo 本身。
如果我不得不猜测,我会说你想做:
但是,我很确定这也会起作用,因为你似乎并不关心返回值:
The return type of session.save(X) is a Serializable representing the identifier of the newly created X, so it should return the id of your HibernateTransitInfo, not the HibernateTransitInfo itself.
If I had to guess, I would say you want to do:
However, I'm pretty sure that this would also work, since you don't appear to care about the return value: