use case is simple: I want to run some boiler plate code before each method in JUnit test annotated with @Test and my custom annotation (let's call it @Mine).
I do not want to use following methods (explanation in parenthesis):
- @RunWith (my test may, or may not use this annotation already, so I cannot assume that I will be able to use my own runner)
- AOP (I cannot make any dependencies to third party libraries, such as AspectJ)
I guess this leaves me with reflection only, which is fine by me. I thought off using @Before accompanied with getting current method via Thread.getCurrentThread() etc. but somehow I find this solution to be a little bit dirty, since I would have to make boiler plate code again within this method to fire reflection (and avoiding any unnecessary code was the goal in the first place).
Maybe you have some other ideas?
I would split the class into two. One with the methods you would have annotated with @mine and one for the others.
Then use @before as normal.
This adds no none standard code and will be easy to understand and maintain for future developers as well.
You need a solution very similar to the answer to Mark unit test as an expected failure, based upon a TestRule. Using the example of a @Deprecated annotation (you can use yours here), you can insert code if the annotation exists on the method. The Description class contains the list of annotations on the method.