Grails: No signature of method findAll() is applic

2020-06-06 04:29发布

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)

2条回答
Summer. ? 凉城
2楼-- · 2020-06-06 05:10

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

查看更多
老娘就宠你
3楼-- · 2020-06-06 05:11

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)
查看更多
登录 后发表回答