Class-Level @Tag Example
Java
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @Tag ( "classTag" ) public class MyTest { @Test @Tag ( "unitTest" ) void exampleOne() { System.out.println( "Executing unit test @Tag" ); } @Test @Tag ( "integrationTest" ) void exampleTwo() { System.out.println( "Executing integration test @Tag" ); } } |
In this above example we have created one class that is MyTest. In above class we have created one tag by @Tag with label classTag. In that class we have created two methods by using @Tag. This Annotation can filter the class as well as methods based on tag names.
JUnit 5 – @Tag
The JUnit 5 testing framework provides a lot of features for testing the application. In those features, Tagging is one of them. This tagging is created using @Tag Annotation JUnit 5. JUnit 5 @Tag is used for filtering the test cases based on the given criteria like characteristics, purpose, and features. @Tag is working like a label in the Test Suite. This @Tag Annotation is used in two different levels. The first one is class level and the other one is method level. This means we can apply this Annotation for both test methods and test classes in the test suite of an application.
- @Tag with class level
- @Tag with method level
Syntax for Test class
@Tag("classTag")
public class MyTestClass {
@Test @Tag("methodTag") void testMethod()
{
// Test logic
}
}
Syntax for Test method
public class MyTestClass {
@Test @Tag("methodTag") void testMethod()
{
// Test logic here
}
}
Now we will discuss both of them with examples.
Contact Us