views:

37

answers:

2

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  A: 

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)
xlson
great and fast answer. Thank you very much!
hering
Good answer, except "Dynamic finders and the other dynamic ORM methods (save, get, count, ..) are all mocked when you call mockDomain(Something) in your unit test" is not entirely true. Some of these methods are missing too, for example FindAllWhereSomeNumberInList.
Fletch
Thanks Fletch, I wasn't aware that that was the case. I'll update the answer to reflect on this. Do you know if there is any good list of what works and what doesn't? To hering: glad to be of help :) I know how hard it can be to find all the information you need when working with a new framework.
xlson
+2  A: 

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

Fletch
I wasn't aware of that, will have check it out. Thanks for the tip!
xlson
I tried it but wasn't able to get it to work. A lot of dependencies are unresolved. I'm trying to test a service which has a findAll with a simple query and some sorting and pagination. What a shame that this isn't possible to test :(
hering