Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Explain how to define a test suite in JUnit 5 with the junit-platform-suite engine, and when a suite class is better than build-tool tag filters.

With the junit-platform-suite dependency we write a class annotated @Suite plus selectors and filters, such as @SelectPackages("com.acme.billing") and @IncludeTags("contract"), and the suite engine runs exactly that selection. A suite is the better choice when the selection itself must be versioned in code, reused by several build tools or run from the IDE; plain build-tool tag filters are simpler for CI stages, and we must stop normal discovery from running the suite's tests a second time.

Why this question is asked

In JUnit 4 a suite was a common way to group tests. JUnit 5 changed the model: the build tool or IDE usually discovers tests and filters them by tags, so many developers never write a suite. Suites still exist, though, and they solve a specific problem well. Interviewers ask this to see whether we know the modern suite engine rather than the old runner, and whether we can judge when a suite adds value and when it only adds a second place to configure the same thing. In this answer, we will set up the suite engine, look at the selectors and filters, decide when a suite beats a tag filter, avoid running tests twice, and look at what happened to the old @RunWith(JUnitPlatform.class) approach.

1A Quick Recap of Engines and Discovery

The JUnit Platform does not run tests itself. It asks test engines to do it. The Jupiter engine runs JUnit 5 style tests, the Vintage engine runs JUnit 4 tests, and the suite engine, from the junit-platform-suite artifact, runs suites. When a build tool starts a run, each engine looks at the classpath and reports the tests it is responsible for.

The suite engine looks for classes annotated @Suite. For each one it reads the selectors (which classes, packages or resources to start from) and the filters (which of those to keep), builds a discovery request of its own, and asks the other engines to run the matching tests as children of the suite.

2Writing a Suite

We add the dependency next to junit-jupiter, with the version managed by the JUnit BOM, and write an empty class that only carries annotations.

XML
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope> <!-- version from junit-bom -->
</dependency>
@Suite
@SuiteDisplayName("Billing contract tests")
@SelectPackages("com.acme.billing")
@IncludeTags("contract")
class BillingContractSuite {}
// Run from the IDE or with: mvn test -Dtest=BillingContractSuite
// Billing contract tests
// InvoiceApiContractTest (5 tests) tagged contract
// RefundEventContractTest (3 tests) tagged contract
// InvoiceServiceTest is in the package but untagged -> not included

Notice that the class body is empty. The annotations are the whole definition, which is why it reads like configuration that happens to live in a Java file. By default, @SelectPackages includes only classes whose names match the usual test pattern (names ending in Test or Tests, or starting with Test), so a helper class in the package is not picked up by accident.

3Selectors and Filters

Selectors say where to start looking and filters narrow the result. We can combine several of each on one suite. @ConfigurationParameter is also useful, because it sets JUnit configuration for this suite only, for example to enable parallel execution just for a contract run.

Common suite annotations
AnnotationKindExample
@SelectPackagesselector@SelectPackages("com.acme.billing")
@SelectClassesselector@SelectClasses({InvoiceApiTest.class})
@SelectClasspathResourceselector@SelectClasspathResource("features/billing")
@IncludeTags / @ExcludeTagsfilter@IncludeTags("contract")
@IncludeClassNamePatternsfilter@IncludeClassNamePatterns(".*ContractTest")
@IncludeEnginesfilter@IncludeEngines("junit-jupiter")
@ConfigurationParametersettingskey = junit.jupiter.execution.parallel.enabled

The annotations below are the ones used most; the full list is in the org.junit.platform.suite.api package and grows a little with each release, so check your version.

4When a Suite Beats a Build-Tool Filter

A tag filter in the POM or build.gradle is the simplest way to say 'this CI stage runs contract tests'. It lives where the pipeline is defined and nobody has to look anywhere else. For most CI stages that is the right choice.

A suite earns its place when the selection itself is a thing we want to version, review and reuse. Three situations come up often. First, when the selection is more complex than one tag, for example contract tests in billing and refunds but not the legacy export package, and we want that definition reviewed in a pull request like code. Second, when the same selection must run from Maven, from Gradle in another repository, and from a developer's IDE: a suite class runs the same way everywhere, while build filters must be repeated in each build. Third, Cucumber and similar engines are usually started through a suite with @SelectClasspathResource and @IncludeEngines("cucumber"), because that is the recommended way to point them at feature files.

So the rule of thumb is: filters for pipeline stages, suites for named, reusable selections.

5Avoiding Running Tests Twice

There is a catch. When Surefire runs all tests, it discovers InvoiceApiContractTest through the Jupiter engine as a normal test, and it also discovers BillingContractSuite through the suite engine, which runs InvoiceApiContractTest again. The report then shows the contract tests twice and the build takes longer.

Text
# Normal run with a suite on the classpath
mvn test
# Tests run: 1258 (1250 tests + 8 contract tests again inside BillingContractSuite)
# Fix: keep suites out of normal discovery, run them only by name
<configuration>
<excludes><exclude>**/*Suite.java</exclude></excludes>
</configuration>
mvn test # Tests run: 1250
mvn test -Dtest=BillingContractSuite # Tests run: 8
Step throughA suite run inside a normal discovery
SurefireJupiter engineSuite engine
discover tests on the classpath
1250 tests, including 8 contract tests
discover suites
select billing package, tag contract
the same 8 contract tests
BillingContractSuite with 8 children

6 messages in order. Play them, or step through them one at a time.

Without an exclude, the 8 contract tests run once directly and once inside the suite.

Another approach is to give suite classes a name that does not match Surefire's default includes, or to run a suite-only stage with a filter on the engine. Whichever we choose, the convention should be written down so new suites follow it.

6What Happened to @RunWith(JUnitPlatform.class)

Before the suite engine existed, the way to group JUnit 5 tests was a JUnit 4 runner, @RunWith(JUnitPlatform.class) from junit-platform-runner. It needed JUnit 4 on the classpath and ran the JUnit Platform inside JUnit 4, which confused reports and IDEs. It was deprecated when the suite engine arrived and has been removed in later releases, at the latest with JUnit 6; check your version. Any remaining uses should be migrated to @Suite, which is usually a matter of swapping the annotations.

7Follow-up questions

Can a suite include another suite?

Yes. Selecting a class annotated @Suite from another suite runs it as a nested suite. It is rarely needed; in most codebases a flat set of named suites is easier to understand.

Do @BeforeAll methods work on a suite class?

Newer versions of the suite engine add suite-level lifecycle methods with @BeforeSuite and @AfterSuite, which run once around the whole suite (check your version). Regular Jupiter @BeforeAll on the suite class does nothing, because the suite class is not a Jupiter test class.

How does a suite appear in the test report?

As a container named after the suite, or after @SuiteDisplayName, with the selected test classes as children. Build tools that flatten reports may show the tests with the suite name as a prefix, so dashboards may count them differently from a normal run.

8Conclusion

In this answer, we saw that the suite engine from junit-platform-suite runs classes annotated @Suite by turning their selectors and filters into a discovery request for the other engines. We wrote a billing contract suite with @SelectPackages and @IncludeTags and looked at the other selectors and filters. Build-tool tag filters remain the simpler choice for CI stages, while suites suit selections that must be versioned in code, shared across build tools or run from the IDE, as long as we keep them out of normal discovery so tests do not run twice. The old @RunWith(JUnitPlatform.class) runner is deprecated and removed in later versions.

What to say in the interviewAbout 90 seconds aloud

Open with

In JUnit 5 a suite is run by its own engine, which comes from the junit-platform-suite artifact. I write an empty class with @Suite and then selectors and filters, for example @SelectPackages com.acme.billing and @IncludeTags contract.

Then, if they want more

The suite engine builds a discovery request from those annotations and asks the Jupiter engine, or any other engine, to run the matching tests as children of the suite. Other selectors are @SelectClasses and @SelectClasspathResource, and filters include @IncludeClassNamePatterns, @ExcludeTags and @IncludeEngines; @ConfigurationParameter sets JUnit options just for that suite.

When do I prefer a suite over tag filters in the build? For ordinary CI stages, I use the build filters because they are simpler and live with the pipeline.

I use a suite when the selection is something we want to version and review in code, when the same selection must run from Maven, Gradle and the IDE without repeating config, or for engines like Cucumber that are normally launched through a suite.

The one thing to watch is double execution: if the suite and its tests are both discovered in a normal run, the tests run twice, so I exclude suite classes from normal discovery and run them by name. And the old @RunWith(JUnitPlatform.class) runner is deprecated and removed in later versions, so I would migrate it to @Suite.

About this answer

Last updated
12 November 2026
Versions it names
JUnit 4, JUnit 5, JUnit 6
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 "Explain how to define a test suite in JUnit 5 with the junit-platform-suite engine, and...": https://scrivio-interview-library.netlify.app/java/junit/226cb7c504/