samedi 15 janvier 2022

Reading custom annotation for JUNIT

I have a custom annotation which I use as config to start off one time set-up for Junit.

@Target(TYPE) @Retention(RUNTIME)
public @interface MyAnnotation{
   String host();
   int port();
}

Test class:

@MyAnnotation(host="0.0.0.0", port=4567)
public class MyTest extends MyAbstractClass{
   @Test
   public void myTest(){
      //do testy things
   }   
}

Superclass:

public class MyAbstractClass{

   @BeforeAll
   public static void start(){
    Config cfg = readConfig();
    //Use config to do one time set-up
   }

   private static Config readConfig(){
      MyAnnotation ann = MyTest.class.getAnnotation(MyAnnotation.class);
      return new Config(ann.host(), ann.port());
   }
}

So currently, I hardcode the name of the test class (MyTest) in readConfig(..). This won't work when I add a second test class.

One way to solve it is:

  • Add another @BeforeAll method in MyTest which will call the @BeforeAll in super-class and pass the class name as a param.

However, I am curious if I can read the name of the executing subclass in the superclass via some reflexion magic. Any ideas are most welcome.

Thanks





Aucun commentaire:

Enregistrer un commentaire