Kartoch
Kartoch

Reputation: 7779

dynamically add annotation to a generated groovy method

I want to generate the test methods in groovy (based on a data set). I use JUnit and I want to add a @Test annotation to each method generated by myself.

So far I have this code:

@BeforeClass
public static void addAutoDetectPluginsTest() {
    AutoDetectPluginsTest.metaClass."test plugin-01" = { -> println("plugin-01")}
}

What is the way to add an annotation @Test to the method test plugin-01 ?

Upvotes: 1

Views: 1062

Answers (1)

blackdrag
blackdrag

Reputation: 6508

your approach with the meta class cannot work. What ever you do in Groovy's meta class system, it does not affect reflection. And JUnit will look for those annotations using reflection. Thus it cannot work. You would probably have to rewrite the bytecode for the class on loading... for example using an agent. But I don't think you want to go that far.

Upvotes: 4

Related Questions