Grails:方法 findAll() 的签名不适用于参数类型:String、ArrayList

发布于 2024-09-24 10:22:47 字数 931 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-10-01 10:22:47

findAll 在单元测试期间不会被模拟,这就是您的代码无法工作的原因。在运行测试之前,您需要手动添加调用的模拟(mockFor 可以帮助您)。如果您使用 HQL 或 Criterias(我建议使用 HQL,而不是纯 HQL),则这适用。

或者,您也可以使用动态查找器来解决问题。当您在单元测试中调用 mockDomain(Something) 时,动态查找器和其他动态 ORM 方法(save、get、count 等)在大多数(?)情况下都会被模拟。它们通常也比 HQL 更容易使用(恕我直言)。

更新:
感谢 Fletch 指出并非所有动态查找器都被嘲笑。不会被模拟的动态查找器的示例如下:Something.findAllWhereSomeNumberInList([1, 2, 3])

您在代码中使用的 HQL 可以使用动态查找器重写如下:

Something.findBySomeNumberLike(searchString)

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:

Something.findBySomeNumberLike(searchString)
风追烟花雨 2024-10-01 10:22:47

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文