Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

metaClass.'static' not working when replacing method

Tags:

groovy

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?

like image 682
Nate Avatar asked Jun 03 '11 02:06

Nate


1 Answers

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.

like image 57
Ted Naleid Avatar answered Oct 21 '22 17:10

Ted Naleid