I posted this in the Grails group but perhaps this is a specific Groovy issue instead.
Is it not possible set a property on an abstract class via the inheriting class and use it? I have a controller that derives from an abstract class with some properties used by the deriving controller class. In the process of attempting to test the controller (an integration test), I can dynamically set properties for the deriving class but they won't stick if the properties belong to the abstract class.
abstract class MyAbstractController
{
MessageSource messageSource;
public def getMessage(id)
{
return messageSource.getMessage(id, null, Locale.ENGLISH)
}
}
class MyFooController extends MyAbstractController
{
def someService;
def show = {
println "messageSource: ${messageSource}"
def msg = getMessage("error.foo.notfound");
println "msg: ${msg}"
}
}
And when attempting to test, I can inject 'someService' via:
// ----- the following doesn't set 'messageSource' on the abstract class -------
def someService = new Expando()'
MyFooController.metaClass.getSomeService = { -> someService }
def messageSource = new StaticMessageSource();
MyAbstractController.metaClass.getMessageSource = { -> messageSource }
// setting it on MyFooController also doesn't change the result
// MyFooController.metaClass.getMessageSource = { -> messageSource }
MyFooController controller = new MyFooController()
controller.show();
The above will print out a non-null messageSource value when inside MyFooController but when you get to getMessage, which is inherited from the parent class, messageSource is now null. I don't understand why the behavior is like this.. Could someone explain this to me?