Grails:方法 findAll() 的签名不适用于参数类型:String、ArrayList
我是 grails 新手,收到以下错误:没有方法签名:Something.findAll() 适用于参数类型:(java.lang.String, java.util.ArrayList) 值:[from Something AS s WHERE s.some_number LIKE ?, [%asdf% ]]"
当我运行test-app
时发生错误。它发生在以下位置:
SomethingVO[] findBySomeNumber(String searchString) {
searchString = "%"+searchString+"%"
return Something.findAll("from Something AS s WHERE s.some_number LIKE ?",[searchString]).collect {
new SomethingVO(it);
}
}
类Something
是一个域对象:
package some.project.domain
class Something{
static belongsTo = [product:Product, productVersion:ProductVersion]
Long id
String name
String someNumber
static constraints = {
product (nullable:true)
productVersion (nullable:true)
}
}
错误在哪里?
(我使用 Grails 1.2.4)
I'm new to grails and receive the following error:No signature of method: Something.findAll() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [from Something AS s WHERE s.some_number LIKE ?, [%asdf%]]"
The error occurs when I run test-app
. It occurs in the following place:
SomethingVO[] findBySomeNumber(String searchString) {
searchString = "%"+searchString+"%"
return Something.findAll("from Something AS s WHERE s.some_number LIKE ?",[searchString]).collect {
new SomethingVO(it);
}
}
The class Something
is a domain object:
package some.project.domain
class Something{
static belongsTo = [product:Product, productVersion:ProductVersion]
Long id
String name
String someNumber
static constraints = {
product (nullable:true)
productVersion (nullable:true)
}
}
Where is the mistake?
(I use Grails 1.2.4)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
findAll 在单元测试期间不会被模拟,这就是您的代码无法工作的原因。在运行测试之前,您需要手动添加调用的模拟(mockFor 可以帮助您)。如果您使用 HQL 或 Criterias(我建议使用 HQL,而不是纯 HQL),则这适用。
或者,您也可以使用动态查找器来解决问题。当您在单元测试中调用
mockDomain(Something)
时,动态查找器和其他动态 ORM 方法(save、get、count 等)在大多数(?)情况下都会被模拟。它们通常也比 HQL 更容易使用(恕我直言)。更新:
感谢 Fletch 指出并非所有动态查找器都被嘲笑。不会被模拟的动态查找器的示例如下:
Something.findAllWhereSomeNumberInList([1, 2, 3])
。您在代码中使用的 HQL 可以使用动态查找器重写如下:
findAll is not mocked during unit testing and that's why your code isn't working. You need to manually add a mock for the call before running your test (mockFor could help you with that). This applies if your use HQL or Criterias (which I would recommend over pure HQL).
Alternatively it's possible that you could solve your problems using dynamic finders. Dynamic finders and the other dynamic ORM methods (save, get, count, ..) are in most(?) cases mocked when you call
mockDomain(Something)
in your unit test. They are also generally easier to use than HQL (imho).Update:
Thanks to Fletch for pointing out that not all dynamic finders are mocked. An example of a dynamic finder that won't be mocked is this:
Something.findAllWhereSomeNumberInList([1, 2, 3])
.The HQL you use in your code could be rewritten like this using dynamic finders:
Xlson 的答案是正确的,但是您可以尝试另一种“尖端”解决方案,该解决方案目前处于测试状态。请参阅 http://grails.1312388.n4.nabble.com/New-approach-to-mocking-domain-classes-in-Grails-unit-tests-td2529895.html
Xlson's answer is correct, however there is an alternative "cutting edge" solution you can try, which is currently in testing status. See http://grails.1312388.n4.nabble.com/New-approach-to-mocking-domain-classes-in-Grails-unit-tests-td2529895.html