Quick Tip: Run Only Specific Tests in Your Build Pipeline with JUnit Tags
At work, I recently had the task to make our build pipeline run only a subset of tests — basically, to skip long-running or low-priority cases.
At first, I went down the Spring Boot @EnabledIf route. It works, but honestly, it’s overkill for this use case. Turns out there’s a much simpler and more elegant way: JUnit Tags.
Tagging Tests
You can tag entire test classes or individual test methods using @Tag()
.
Example:
@Tag("prio")
class TestA {
// all tests in here are part of "prio"
}
Or tag specific methods:
class TestB {
@Tag("prio")
@Test
void fastCase() {}
@Test
void slowCase() {}
}
Run Only Tagged Tests
To execute only the tests you tagged (in this case, with prio
), just update your build pipeline to include the following Maven option:
mvn test -Dgroups=prio
That’s it — Maven will only run the tagged tests.
Why It’s Great
- Super simple, no framework-specific hacks.
- Works out of the box with JUnit 5.
- Easy to manage test categories (e.g.
fast
,integration
,flaky
).
No extra annotations, no YAML magic. Just clean, native JUnit Tags.
Happy testing.