无法使用具有 createCriteria() 语句的函数执行单元测试
我想编写一个单元测试(通过 JUnit)来测试 Groovy 中该函数的值:
String getPeopleNamesById(int[] peopleIds) {
List<String> names = People.createCriteria().list{
projections { property("name") }
'in' ("id", peopleIds)
}
return names ? names.join(", ") : "";
}
但是在读取以下语句时单元测试总是失败:List names = People.createCriteria().list{...} groovy.lang.MissingMethodException:没有方法签名:People.createCriteria() 适用于参数类型:() 值:[]。 我猜是因为调用了执行一些数据库连接和查询的函数? 你能帮我写这个测试吗?太感谢了!
I want to write a unit test (by JUnit) to test value of this function in Groovy:
String getPeopleNamesById(int[] peopleIds) {
List<String> names = People.createCriteria().list{
projections { property("name") }
'in' ("id", peopleIds)
}
return names ? names.join(", ") : "";
}
But the unit test always fails when reading this statement: List names = People.createCriteria().list{...}
groovy.lang.MissingMethodException: No signature of method: People.createCriteria() is applicable for argument types: () values: [].
I guess it because of calling to functions which execute some DB connections and queries?
Could you please help me write this test? Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
标准查询在单元测试中不可用,并且
mockDomain
不提供。您可以自己模拟您的条件查询,例如使用mockFor
,或者使您的测试成为集成测试,您可以访问完整的数据库环境。以下是如何模拟查询的示例:
Criteria queries aren't available in unit tests, and aren't provided by
mockDomain
. You can either mock your criteria queries yourself, e.g. withmockFor
, or make your test an integration test, where you have access to a full database environment.Here's an example of how you could mock your query: