CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-junit-platform--junit-platform-suite-api

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

Pending
Overview
Eval results
Files

lifecycle.mddocs/

Suite Lifecycle Management

Annotations for controlling execution flow with setup and teardown methods that run before and after all tests in a suite.

Capabilities

@BeforeSuite Annotation

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:

  • Must be static
  • Must return void
  • Must not be private
  • May have any visibility (public, protected, package-private)

Usage 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
}

@AfterSuite Annotation

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:

  • Must be static
  • Must return void
  • Must not be private
  • May have any visibility (public, protected, package-private)

Usage 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();
    }
}

Lifecycle Execution Order

Multiple Methods in Same Class

When multiple @BeforeSuite or @AfterSuite methods exist in the same class:

  • Execution order is deterministic but not obvious
  • Do not rely on specific ordering between methods in the same class
  • Use method dependencies or combine logic into a single method if order matters

Inheritance Hierarchy

For inherited lifecycle methods:

  • @BeforeSuite: Superclass methods execute before subclass methods
  • @AfterSuite: Subclass methods execute before superclass methods (reverse order)
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

Best Practices

Resource Management

@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();
        }
    }
}

Exception Handling

@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());
        }
    }
}

Configuration and State

@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

docs

configuration.md

core-framework.md

filtering.md

index.md

lifecycle.md

selection.md

tile.json