Reputation: 1002
I'm new to grails and trying to write unit tests for Service class has the method, it calls criteria on domain object.
How do you mock the domain behavior in the following method of a service class?
{def checkForDuplicates(firstName, lastName, gender, birthDate, accountId){
def duplicateIndividual = Individual.withCriteria{
eq('firstName',firstName)
eq('lastName',lastName)
eq('gender',gender)
eq('birthDate',birthDate)
and{
accounts{
eq('id', accountId)
}
}
}
if(duplicateIndividual){
log.error("Found duplicate for ${duplicateIndividual.firstName}
${duplicateIndividual.lastName}")
return true
}
return false
}"
Upvotes: 0
Views: 1345
Reputation: 3532
In grails 2, you can use the Mock() functionality and mockDomain to set up your criteria,
@TestFor(MyService)
@Mock(Individual)
Then you just create domain classes normally as you would.
http://grails.org/doc/latest/guide/single.html#unitTestingDomains
Prior to grails 2, you can change the metaClass of your query, something like
Individual.metaClass.static.withCriteria = { [ i1, i2 ] }
Upvotes: 1