Skip to content

Document StoreScope enum and getStore(StoreScope, Namespace) in 5.13.x User Guide #4784

@JKomoroski

Description

@JKomoroski

Problem

The StoreScope enum and ExtensionContext.getStore(StoreScope, Namespace) method introduced in JUnit 5.13 are not documented in the User Guide. This new feature for resource lifecycle management is only discoverable through API documentation and source code inspection.

Impact

Developers encountering resource management issues (especially with JUnit 5.13's enhanced AutoCloseable cleanup behavior) cannot easily discover this solution. The lack of documentation leads to:

  • Workarounds like disabling junit.jupiter.extensions.store.close.autocloseable.enabled
  • Incorrect usage of GLOBAL namespace thinking it provides JVM-wide scope
  • Frustration when resources get unexpectedly closed during test execution
  • Missing the intended solution for session-scoped and request-scoped resource management

Current State

What exists:

  • API documentation for StoreScope enum with minimal descriptions
  • Brief mention in JUnit 5.13.0 release notes: "resource management mechanism that allows preparing and sharing state across executions or test engines via stores that are scoped to a LauncherSession or ExecutionRequest"

What's missing:

  • User Guide documentation explaining when and how to use each scope
  • Examples showing practical use cases
  • Migration guidance for common resource management patterns

Requested Documentation

Add a section to the User Guide covering:

1. StoreScope Overview

  • Explanation of the three scopes: EXTENSION_CONTEXT, LAUNCHER_SESSION, EXECUTION_REQUEST
  • Lifecycle diagrams showing when each scope opens/closes
  • Relationship to AutoCloseable resource cleanup

2. Practical Examples

// Session-scoped database connection shared across all tests
@Override
public void beforeAll(ExtensionContext context) {
    Store store = context.getStore(StoreScope.LAUNCHER_SESSION, 
                                   Namespace.create("database"));
    
    DataSource dataSource = store.getOrComputeIfAbsent("connection", 
        key -> createDataSource(), DataSource.class);
}

// Request-scoped test container shared across test engines
Store store = context.getStore(StoreScope.EXECUTION_REQUEST, 
                               Namespace.create("containers"));

3. Common Use Cases

  • Shared test infrastructure (databases, containers, external services)
  • Expensive resource initialization that should survive test class boundaries
  • Cross-engine state sharing
  • Migration from static fields to proper lifecycle management

4. Best Practices

  • When to use each scope vs traditional getStore(Namespace)
  • Namespace selection strategies to avoid conflicts
  • Resource cleanup considerations
  • Thread safety implications

Suggested Location

Add as a new subsection under "5.13. Keeping State in Extensions" titled "5.13.x Resource Scoping with StoreScope" or integrate into existing store documentation.

Additional Context

This feature addresses a significant gap in JUnit's resource management story and represents the intended solution for many common testing scenarios involving shared infrastructure. Better documentation would help developers adopt this pattern instead of anti-patterns like static resource management.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions