0
votes

I'm using grails 2.3.7 .. in a Spock unit test i'm mocking a findWhere method ..

  DocumentHeader.metaClass.static.findWhere = {DocumentType type, String workStation, Boolean complete ->
            println "Running mock findWhere .. "
            new DefaultDocument()
        }

which i'm using to mock a method call in a service ..

def returnDocument =  DocumentHeader.findWhere(documentType:DocumentType.DEFAULT_TYPE,
                        workStation: requirement.workstation,
                        complete: false)

The parameter types are correct but when invoke the test I get

Cannot query [com.sample.DocumentHeader] on non-existent property: workStation org.springframework.dao.InvalidDataAccessResourceUsageException: Cannot query [com.vantec.DocumentHeader] on non-existent property: workStation
at org.grails.datastore.mapping.simple.query.SimpleMapQuery

so it appears to be invoking the real method - not the mock .. Anyone any ideas ? Don't recall mocking a findWhere query before so anyone know of any problems ? TIA ..

1

1 Answers

0
votes

By this code, you are mocking (or rather adding) method

DocumentHeader findWhere(DocumentType dt, String w, Boolean c)

But in you service you are calling

DocumentHeader findWhere(Map props)

Try changing your metaClass to:

DocumentHeader.metaClass.static.findWhere = {Map props ->
    println "Running mock findWhere .. "
    new DefaultDocument()
}