I just tried to write this simple code to test overriding methods using metaClass.
The code is here:
class Hello {
public Hello()
{
Foo()
}
public void Foo()
{
println "old"
}
}
It has a Foo() method which simply prints "old" and it was called by the constructor.
Here's the test code:
class HelloTest {
@Test
public void test() {
boolean methodFooWasCalled = false
Hello.metaClass.Foo = {-> println "new"
methodFooWasCalled = true
}
Hello hello = new Hello()
assertTrue methodFooWasCalled == true
}
}
I was expecting that the output should be "new" since Foo()
has been overriden. But it still printed "old". Does anyone know why it fails? Thanks