You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Yasin Diouri <am...@gmail.com> on 2016/04/08 22:54:53 UTC

Replaced static method not replaced only when called from constructor

Hello!

I have a strange issue which I already posted on StackOverflow
<http://stackoverflow.com/questions/36501291/replaced-static-method-not-replaced-only-when-called-from-constructor>
but I was advised to send it to this mailing list as well.

class ClassA {
    static String getA() {
        'A'
    }

    String string

    ClassA() {
        println getA()
        println ClassA.getA()
        string = getA()
    }}

// MockingClassA.metaClass.static.getA = { -> 'B'
}ClassA.metaClass.getA = { -> 'B' }assert ClassA.getA() == 'B'
def test = new ClassA()assert test.getA() == 'B'assert test.string == 'B'

Resuming: calling getA() from anywhere but the constructor calls the
replaced implementation. To be able to call the new implementation from the
constructor you have to use the long form ClassA.getA().

Kind regards,
Yassine Diouri