2

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
    }

}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73

2 Answers2

3

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.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

Nope. Out of curiosity, what would lead you to believe that you would need to do that?

Gregg
  • 34,973
  • 19
  • 109
  • 214