Nate
Nate

Reputation: 111

metaClass.'static' not working when replacing method

I'm using groovy 1.7.8.

I have the following code:

public class StaticClass {
    public static String getStaticString(String string) {
        return "NOT WORKING"
    }
}

My test:

void testStaticMethod() {
    StaticClass.metaClass.'static'.getStaticString = { i ->
        "WORKING"
    }

    assert "WORKING" == StaticClass.getStaticString('test')
}

I can not get my test to pass. Any ideas on what I'm doing wrong?

Upvotes: 11

Views: 6052

Answers (1)

Ted Naleid
Ted Naleid

Reputation: 26821

Try typing the closure:

StaticClass.metaClass.'static'.getStaticString = { String i ->
    "WORKING"
}

You need to match the method signature exactly if you're trying to override something.

Upvotes: 28

Related Questions