In my controller, i have an action which uses criteria to hit db and fetch results.
def c = DomainObj.createCriteria()
def result =[]
result = c.list(params) {
'eq'("employerid", id)
}
I am trying to mock this criteria in my unit test.
def mycriteria =[
list: {Closure cls -> new DomainObj(id:1)}
] ]
DomainObj.metaClass.static.createCriteria = {mycriteria}
The above does not work. It throws out an exception when c.list(params) is being executed. The exception is “groovy.lang.MissingMethodException: No signature of method: testSearch_closure3.docall() is applicable for arguement types:
PS- However, if i remove params from c.list() in controller i.e. see below:
def c = DomainObj.createCriteria()
def result =[]
result = c.list() {
}
then, it is working. Not sure what the problem here is. Any help is appreciated
This is because of the default parametes of the
listmethod.e.g.
above can be used like:
you change
metaClassand add methodlistthat takes only one parameter.So your
mycriteriashould look like this:consider this example:
output is:
EDIT
To change returned object you do as previously: