Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Tag slow integration tests with @Tag("integration") and show how to run only fast tests locally and all tests in CI with Maven Surefire and with Gradle. How do tag expressions work?

We mark slow tests with @Tag("integration") on the class or method, then tell the build which tags to run: Maven Surefire takes <excludedGroups> or <groups>, usually set in profiles, and Gradle takes includeTags and excludeTags inside useJUnitPlatform. Both accept JUnit tag expressions, which combine tags with !, & and |, such as "integration & !slow". A composed annotation such as @IntegrationTest that carries the tag avoids typos.

Why this question is asked

Fast feedback is the reason tags exist. A developer who has to wait eight minutes for a database-backed suite before every commit soon stops running tests locally. Interviewers ask this because it checks that we can wire JUnit 5 into real builds, not only write tests, and that we know the tag expression language that both Maven and Gradle pass to the JUnit Platform. In this answer, we will tag the tests, configure Maven with profiles, configure Gradle with a separate task, look at tag expressions in detail, and finish with a composed annotation that keeps the tag names consistent.

1A Quick Recap of Tags

A tag is a label we attach to a test class or a test method with @Tag. It does not change how the test runs. It only gives the JUnit Platform, the part of JUnit that discovers and runs tests for build tools and IDEs, something to filter on. Tag names are plain strings; they must not be blank and must not contain spaces, commas, parentheses, &, | or !, because those characters belong to the expression syntax.

Tags are inherited. A tag on a class applies to every test method in it, and @Nested classes inherit the tags of the class around them. So one annotation on the class is usually enough.

Java
@Tag("integration")
class OrderRepositoryTest {
@Test void savesOrder() { ... } // tags: integration
@Tag("slow")
@Test void importsMillionRows() { ... } // tags: integration, slow
@Nested class WhenEmpty {
@Test void findsNothing() { ... } // tags: integration (inherited)
}
}
class PriceCalculatorTest {
@Test void addsTax() { ... } // no tags: a fast unit test
}

2Running Only Fast Tests With Maven Surefire

Surefire, the Maven plugin that runs tests in the test phase, maps its groups and excludedGroups parameters onto JUnit tags. We put the default, which excludes integration tests, in the main configuration and add a ci profile that runs everything. Use a current Surefire 3.x.

XML
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
<configuration>
<excludedGroups>integration</excludedGroups> <!-- local default: fast tests only -->
</configuration>
</plugin>
<profiles>
<profile>
<id>ci</id>
<build><plugins><plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedGroups combine.self="override"/> <!-- run every tag -->
</configuration>
</plugin></plugins></build>
</profile>
</profiles>
<!-- mvn test -> PriceCalculatorTest only
mvn test -Pci -> all tests
mvn test -Dgroups=integration -> only integration tests -->

Notice the last command. groups and excludedGroups are also user properties, so we can override them on the command line without editing the POM.

Many teams prefer a different split: name integration test classes *IT and let the Failsafe plugin run them in the integration-test phase during mvn verify. Then mvn test is fast by default and mvn verify runs everything. Tags and Failsafe combine well: Failsafe can also take groups, for example to run only the contract-tagged ITs in one CI stage.

3Running Only Fast Tests With Gradle

In Gradle we configure the standard test task to exclude the tag, and register a second task of type Test that includes it. The second task reuses the compiled test classes and classpath of the main test source set.

Java
// build.gradle
tasks.named('test', Test) {
useJUnitPlatform {
excludeTags 'integration' // ./gradlew test -> fast tests only
}
}
tasks.register('integrationTest', Test) {
description = 'Runs tests tagged integration'
group = 'verification'
testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath
useJUnitPlatform {
includeTags 'integration' // ./gradlew integrationTest
}
shouldRunAfter tasks.named('test')
}
tasks.named('check') { dependsOn 'integrationTest' } // ./gradlew check -> everything (CI)

So a developer runs ./gradlew test for quick feedback, and CI runs ./gradlew check, which now depends on both tasks. Gradle's JVM test suites plugin offers a more structured version of the same idea with a separate source set; the tag approach is simpler when the tests already live together.

4How Tag Expressions Work

Both build tools pass the strings we give them to the JUnit Platform, which parses them as tag expressions. An expression combines tag names with three operators: ! for not, & for and, | for or. Parentheses group parts, and ! binds tightest, then &, then |. Two special expressions exist too: any() matches tests with at least one tag and none() matches tests with no tags.

Text
# Maven: mvn test -Dgroups="integration & !slow"
# Gradle: includeTags 'integration & !slow'
integration & !slow # OrderRepositoryTest.savesOrder, WhenEmpty.findsNothing
integration | contract # anything tagged integration or contract
!(integration | e2e) # everything except those two
none() # PriceCalculatorTest.addsTax: untagged tests only
Which tests each filter selects
FilteraddsTaxsavesOrderimportsMillionRows
excludedGroups=integrationrunsskippedskipped
groups=integrationskippedrunsruns
groups=integration & !slowskippedrunsskipped
groups=none()runsskippedskipped

Notice that none() is a neat way to say 'pure unit tests' if the convention is that unit tests carry no tag. In Maven we must quote the expression on the command line, because the shell treats & and ! specially.

5A Composed Annotation to Avoid Typos

Tag names are strings, so @Tag("integraton") with a typo compiles happily and the test silently runs in the fast suite, or never runs in the CI stage that looks for the right spelling. JUnit supports meta-annotations: an annotation of our own that carries @Tag and anything else the tests always need. For Spring Boot integration tests it can carry @SpringBootTest too.

Java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Tag("integration")
@SpringBootTest
@ActiveProfiles("test")
public @interface IntegrationTest {}
@IntegrationTest
class OrderApiTest { // tags: integration, plus a full Spring context
@Test void createsOrder() { ... }
}

Now the tag name lives in one place, a typo becomes a compile error, and every integration test shares the same Spring configuration, which also helps Spring reuse one cached application context.

6Follow-up questions

Does a tag on a superclass apply to subclasses?

Yes. @Tag is inherited, so a tag on an abstract base test class applies to its subclasses. That is another way to share tags, though a composed annotation is usually clearer because it is visible on the test class itself.

Can I select tags from the IDE?

Yes. IntelliJ IDEA and Eclipse let us create a run configuration that runs tests by tag or tag expression, because they also talk to the JUnit Platform. That is handy for running only the integration tests of one module.

What happens if the tag expression is invalid?

The JUnit Platform rejects it when the run starts and reports a parse error, so the run fails instead of selecting nothing. A wrong but valid tag name, however, simply matches no tests, which is why consistent names through a composed annotation matter.

7Conclusion

In this answer, we tagged slow tests with @Tag("integration"), relying on inheritance from classes to methods and nested classes. We excluded them from the default Maven run with excludedGroups and enabled them in a ci profile, and in Gradle we excluded them from test and added an integrationTest task wired into check. We saw that tag expressions combine names with !, & and |, plus any() and none(), and we used a composed @IntegrationTest annotation so the tag name cannot be misspelled.

What to say in the interviewAbout 80 seconds aloud

Open with

I would put @Tag("integration") on each slow test class; tags are inherited by the methods and by nested classes, so one annotation per class is enough. Better still, I would create a composed annotation, @IntegrationTest, that carries the tag and @SpringBootTest, so nobody can misspell the tag and all integration tests share one context configuration.

Then, if they want more

In Maven, I would set excludedGroups to integration in the Surefire configuration, so a plain mvn test runs only fast tests, and add a ci profile that clears it. groups and excludedGroups are also user properties, so I can run -Dgroups=integration from the command line. An alternative is naming them *IT and letting Failsafe run them in verify.

In Gradle, I would configure the test task with useJUnitPlatform and excludeTags integration, then register an integrationTest task of type Test that uses the same test classes and classpath with includeTags integration, and make check depend on it, so CI runs ./gradlew check.

Both tools hand these strings to the JUnit Platform as tag expressions, which support not, and, or with parentheses, so I can write something like integration and not slow, and none() selects untagged unit tests.

About this answer

Last updated
12 November 2026
Versions it names
JUnit 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JUnit user guide
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Tag slow integration tests with @Tag("integration") and show how to run only fast tests...": https://scrivio-interview-library.netlify.app/java/junit/85e6b2f9f5/