JUnit Platform Suite API provides annotations and configuration options for creating test suites with the JUnit Platform, including test selection, filtering, and configuration capabilities for organizing and executing collections of tests
—
Annotations for controlling execution flow with setup and teardown methods that run before and after all tests in a suite.
Signals that the annotated method should be executed before all tests in the current test suite.
/**
* Signals that the annotated method should be executed before all tests
* in the current test suite.
*
* Methods must be static, void, and not private.
* Supports inheritance with deterministic but non-obvious execution order.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@API(status = EXPERIMENTAL, since = "1.11")
public @interface BeforeSuite {
}Method Requirements:
staticvoidprivateUsage Examples:
@Suite
@SelectPackages("com.example.integration")
public class IntegrationTestSuite {
@BeforeSuite
static void setupDatabase() {
System.setProperty("test.database.url", "jdbc:h2:mem:testdb");
DatabaseTestUtil.initializeSchema();
}
@BeforeSuite
static void setupHttpClient() {
HttpClientTestUtil.configureTestClient();
}
}
// Inheritance example
public abstract class BaseTestSuite {
@BeforeSuite
protected static void baseSetup() {
System.out.println("Base suite setup");
}
}
@Suite
@SelectClasses(MyTest.class)
public class ConcreteTestSuite extends BaseTestSuite {
@BeforeSuite
static void concreteSetup() {
System.out.println("Concrete suite setup");
}
// Both baseSetup() and concreteSetup() will execute
}Signals that the annotated method should be executed after all tests in the current test suite.
/**
* Signals that the annotated method should be executed after all tests
* in the current test suite.
*
* Methods must be static, void, and not private.
* Supports inheritance - superclass methods execute after subclass methods.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@API(status = EXPERIMENTAL, since = "1.11")
public @interface AfterSuite {
}Method Requirements:
staticvoidprivateUsage Examples:
@Suite
@SelectPackages("com.example.integration")
public class IntegrationTestSuite {
@BeforeSuite
static void setupResources() {
DatabaseTestUtil.initializeSchema();
MessageQueueTestUtil.startEmbeddedBroker();
}
@AfterSuite
static void cleanupResources() {
MessageQueueTestUtil.stopEmbeddedBroker();
DatabaseTestUtil.cleanupSchema();
}
}
// Multiple cleanup methods
@Suite
@SelectTags("performance")
public class PerformanceTestSuite {
@AfterSuite
static void generateReport() {
PerformanceReportGenerator.generateReport();
}
@AfterSuite
static void cleanupTempFiles() {
TestFileUtil.cleanupTempDirectory();
}
}When multiple @BeforeSuite or @AfterSuite methods exist in the same class:
For inherited lifecycle methods:
public abstract class BaseTestSuite {
@BeforeSuite
static void baseSetup() {
System.out.println("1. Base setup");
}
@AfterSuite
static void baseCleanup() {
System.out.println("4. Base cleanup");
}
}
@Suite
@SelectClasses(MyTest.class)
public class ConcreteTestSuite extends BaseTestSuite {
@BeforeSuite
static void concreteSetup() {
System.out.println("2. Concrete setup");
}
@AfterSuite
static void concreteCleanup() {
System.out.println("3. Concrete cleanup");
}
}
// Execution order: 1 -> 2 -> [tests] -> 3 -> 4@Suite
@SelectPackages("com.example.database")
public class DatabaseTestSuite {
private static TestDatabase testDatabase;
@BeforeSuite
static void startDatabase() {
testDatabase = new TestDatabase();
testDatabase.start();
testDatabase.loadTestData();
}
@AfterSuite
static void stopDatabase() {
if (testDatabase != null) {
testDatabase.stop();
}
}
}@Suite
@SelectTags("integration")
public class IntegrationTestSuite {
@BeforeSuite
static void setupExternalServices() {
try {
ExternalServiceTestUtil.startMockServices();
} catch (Exception e) {
throw new RuntimeException("Failed to start external services", e);
}
}
@AfterSuite
static void cleanupExternalServices() {
try {
ExternalServiceTestUtil.stopMockServices();
} catch (Exception e) {
// Log but don't throw - allow other cleanup to proceed
System.err.println("Warning: Failed to stop external services: " + e.getMessage());
}
}
}@Suite
@SelectClasses({SecurityTest.class, AuthenticationTest.class})
public class SecurityTestSuite {
@BeforeSuite
static void configureSecurityContext() {
System.setProperty("security.test.mode", "true");
SecurityTestUtil.installTestSecurityManager();
}
@AfterSuite
static void restoreSecurityContext() {
SecurityTestUtil.uninstallTestSecurityManager();
System.clearProperty("security.test.mode");
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-junit-platform--junit-platform-suite-api