Verify test execution filtering using @Tag annotation
Preconditions (2)
✅ Expected Result: Only tests with the specified tag are executed in each run
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class TagExampleTest { @Test @Tag("fast") void fastTest() { assertTrue(true, "Fast test should always pass"); } @Test @Tag("slow") void slowTest() { assertTrue(true, "Slow test should always pass"); } @Test void untaggedTest() { assertTrue(true, "Untagged test should always pass"); } } /* To run tests with tag 'fast', use: mvn test -Dgroups=fast or configure your IDE or build tool to include only @Tag("fast") tests. This example shows how to categorize tests and run them selectively. */
This code defines three test methods: one tagged as fast, one as slow, and one without any tag.
The @Tag annotation categorizes tests. You can run tests filtered by these tags using your build tool or IDE settings.
Assertions simply verify the tests run and pass. The untagged test runs unless filtered out.
This setup helps organize tests by speed or type, making test runs faster and more focused.
Add tests with multiple tags and run tests that match any of the tags
@Tag annotation in JUnit?@Tag@Tag is used to label tests with categories or groups.@Tag groups tests = A [OK]@Tag("integration")
class DatabaseTests {
@Test void testConnection() {}
@Test @Tag("slow") void testQueryPerformance() {}
}
@Tag("unit")
class ServiceTests {
@Test void testServiceLogic() {}
}@Test
@Tag(fast)
void quickTest() {}Pass the test and this pattern moves unknown