mockDomain provides dynamic methods like save(), validate(), … for a domain class.
Is it necessary to remove the meta classes for each class I mock using mockDomain?
class UserTests extends GrailsUnitTestCase {
protected void setUp()
{
super.setUp()
mockDomain User
mockDomain Address
}
protected void tearDown()
{
super.tearDown()
def remove = GroovySystem.metaClassRegistry.&removeMetaClass
remove User
remove Address
}
}
You don’t need to do that. The mockDomain method calls
registerMetaClass, which stashes away the current metaClass and substitutes a new one, so that on tearDown the test can restore the old metaClass for you. When you do need to add methods to a metaClass yourself, you can call registerMetaClass (before you add your changes, of course) and once it’s done the test will do the cleaning up.