@Tag Example
Let’s create example classes with A, B, C tags and group them using tags.
testClass1 with A and B tag
package com.aditya04848.junit.helper;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Tag;
public class testClass1
{
@Test
@Tag("A")
@Tag("B")
public void test(){
System.out.println("Test Class 1");
assertTrue(true);
}
}

You can also try this code with Online Java Compiler
Run Code
testClass2 with B and C tag
package com.aditya04848.junit.helper;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Tag;
public class testClass2
{
@Test
@Tag("B")
@Tag("C")
public void test() {
System.out.println("Test Class 2");
assertTrue(true);
}
}

You can also try this code with Online Java Compiler
Run Code
testClass3 with C and A tag
package com.aditya04848.junit.helper;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Tag;
public class testClass3
{
@Test
@Tag("C")
@Tag("A")
public void test(){
System.out.println("Test Class 3");
assertTrue(true);
}
}

You can also try this code with Online Java Compiler
Run Code
testSuite1 with A tags
We will create a test suite that will group all classes with an A tag.
package com.aditya04848.junit.helper;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
@Suite
@SelectPackages("com.aditya04848.junit.helper")
@IncludeTags("A")
public class testSuite1{
}

You can also try this code with Online Java Compiler
Run Code
testSuite2 with B tags
This test suite class groups all classes with a B tag.
package com.aditya04848.junit.helper;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
@Suite
@SelectPackages("com.aditya04848.junit.helper")
@IncludeTags("B")
public class testSuite2{
}

You can also try this code with Online Java Compiler
Run Code
testSuite3 with C tags
This test suite class groups all classes with a C tag.
package com.aditya04848.junit.helper;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
@Suite
@SelectPackages("com.aditya04848.junit.helper")
@IncludeTags("C")
public class testSuite3{
}

You can also try this code with Online Java Compiler
Run Code
FAQs
-
How to use @tag In JUnit 5?
JUnit 5 @Tag can filter test cases from test suites. We can provide tags to different classes and tests and filter them as per our needs.
-
What is JUnit?
JUnit is a simple, open-source framework to write and run repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
-
What is @Test annotation in JUnit?
The annotated methods represent the test cases in the class. Each annotated with @Test in a JUnit class, there could be multiple methods. This implies that a class may have numerous test cases.
-
When to use @before/@beforeeach annotation in JUnit?
This annotation can be used before each test’s initiation, so the code under this annotation will execute before each test.
Key Takeaways
In this article, we have extensively discussed Tagging and Filtering JUnit Tests. We have learned how to use the @Tag to tag a class or a test. We have cerated example classes with different tags and created test suites corresponding to those tags.
We hope that this blog has helped you enhance your knowledge regarding Tagging and Filtering JUnit Tests and if you would like to learn more, check out our articles on JUnit exception Test, JUnit Time Test. Do upvote our blog to help other ninjas grow.
Check out JUnit Interview Questions here.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Happy Reading!