Grails: No signature of method findAll() is applicable for argument types: String, ArrayList

喜你入骨 提交于 2019-12-04 14:18:31

问题


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)


回答1:


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)



回答2:


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



来源:https://stackoverflow.com/questions/3758347/grails-no-signature-of-method-findall-is-applicable-for-argument-types-strin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!