4

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):

  1. @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)
  2. 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?

3
  • I don't understand why @Before can't be used Nov 22, 2011 at 21:15
  • Because, as far as I know, Before is being run before each method annotated with Test. My expected behaviour is different. I would like my custom code to be run on methods marked with Mine annotation. It cannot depend on other annotations. Nov 24, 2011 at 14:59
  • @Before does get run before each and every method. Nov 24, 2011 at 15:27

2 Answers 2

12

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.

public class ExecutionTest {
    public class BeforeExecution implements TestRule {
        public Statement apply(Statement base, Description description) {
            return statement(base, description);
        }

        private Statement statement(final Statement base, final Description description) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    if (description.getAnnotation(Deprecated.class) != null) {
                        // you can do whatever you like here.
                        System.err.println("this will be run when the method has an @Deprecated annotation");
                    }
                    base.evaluate();
                }
            };
        }
    }

    @Rule public BeforeExecution beforeExecution = new BeforeExecution();

    // Will have code executed.
    @Deprecated
    @Test public void test1() {
         // stuff
    }

    // won't have code executed.
    @Test public void test2() {
         // stuff
    }
}
3
  • Confirmed. This works as expected. My final solution was to write abstract class having proper '@Rule' field. Each of mine tests extends this class so the only thing left to do is to use my proper '@Mine' annotation whenever I want. Nov 27, 2011 at 16:30
  • Does it harm to make the inner class static i.e. public static class BeforeExecution implements TestRule ? Jan 25, 2014 at 9:35
  • @Mr_and_Mrs_D No, it does not. Jan 25, 2014 at 10:02
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.