mercredi 12 octobre 2016

Getting groovy method annotation using reflection

I am writing a Reporter for spock test that writes annotations to a file - heavily inspired by QCIntegration. My problem is getting the method under test. The test method is named using a string:

@MyOwnAnnotation(firstParameter = "1st")
def "a test method"() {}

I need to get the annotation:

class TestBase {
    @Rule
    public TestName testName = new TestName()

    @Before
    def getAnnotation() {
        try {
            Method method = this.getClass().methods.find {
                it.name == testName.methodName
            }

            if (method.isAnnotationPresent(TestRequirementInfo.class)) {
                testRequirementInfo = method.getAnnotation(TestRequirementInfo.class)
            }
        } catch ...
    }
}

This code does not work because testName.methodName contains the string a test method while the methods in this.getClass().methods are java names and I guess the method I am looking for has got an auto created name like $spock_feature_2_1.

So my question is; how do I get the annotation from a groovy method using reflection?





Aucun commentaire:

Enregistrer un commentaire