views:

23

answers:

0

I'd like to override the constructor of a class for testing. I can do it like this:

 SomeClass.metaClass.constructor = { Map params -> 
     def instance = BeanUtils.instantiateClass(SomeClass)
     instance.apply(params)
     instance
 }

That works, but I need the new constructor to apply to only some instances. In particular, I'd like to limit the scope of this change to a closure. I tried to make a category:

class SomeClassCategory {
    static def constructor(instance, params) { }
}

use(SomeClassCategory) {
    def x = new SomeClass(params)
}

But that creates a method called constructor instead of an actual constructor. Is there anyway to specify a constructor in a category? Or can I apply changes to the metaClass of SomeClass only within a block like the use(Category) construct?