Wednesday 14 July 2021

Junit5: Meta Annotations

JUnit Jupiter annotations can be used as meta-annotations. That means that you can define your own composed annotation that will automatically inherit the semantics of its meta-annotations.

 

SpeedTest.java

package com.sample.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Tag("fast")
@Tag("quick")
@Test
public @interface SpeedTest {

}

 

Since meta annotations carry semantics, you can use SpeedTest annotation to run the test cases.

 

MetaAnnotationDemo.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class MetaAnnotationDemo {
  
  @SpeedTest
  public void speedTest1() {
    assertTrue(true);
  }

}



Previous                                                    Next                                                    Home

No comments:

Post a Comment