Testing / JUnit6 Interview Questions
What is the @Suite API in JUnit 6 and how do you group tests into suites?
JUnit 6 provides a @Suite annotation (in junit-platform-suite) for declaratively grouping tests from multiple classes or packages. This replaces JUnit 4's @RunWith(Suite.class) and JUnit Platform Runner (which was removed in JUnit 6).
import org.junit.platform.suite.api.*; // Basic suite: select specific test classes @Suite @SelectClasses({ OrderServiceTest.class, PaymentServiceTest.class, InventoryServiceTest.class }) class SmokeTestSuite {} // Suite by package (runs all tests in the package) @Suite @SelectPackages({"com.example.unit", "com.example.integration"}) class AllTestsSuite {} // Suite with tag filtering @Suite @SelectPackages("com.example") @IncludeTags("fast") @ExcludeTags({"slow", "integration"}) class FastTestSuite {} // Suite with class name pattern @Suite @SelectPackages("com.example") @IncludeClassNamePatterns(".*IT") // only classes ending in IT class IntegrationTestSuite {} // Maven: run suites explicitly // <configuration> // <includes> // <include>**/*Suite.class</include> // </includes> // </configuration>
Important: suite classes themselves contain no test methods. They are purely declarative configuration classes. The @Suite annotation is a signal to the JUnit Platform to aggregate and run the selected tests.
More Related questions...