Skip to content

Change default ExtensionContextScope to TEST_METHOD and allow extensions to opt out #5086

@marcphilipp

Description

@marcphilipp

Context

TestInstantiationAwareExtension and ExtensionContextScope were introduced in 5.12 to enable use cases such as field injection in nested test classes. For backward compatibility, by default, extensions extending TestInstantiationAwareExtension will receive an ExtensionContext that is scoped to the test class. Extensions may override the getTestInstantiationExtensionContextScope(...) method to receive an ExtensionContext that is scoped to the test method instead. Moreover, the junit.jupiter.extensions.testinstantiation.extensioncontextscope.default configuration parameter allows to change the default to TEST_METHOD for extensions returning DEFAULT from that method (as done by the default implementation).

Motivation

There is currently no way for an extension to signal that it needs an ExtensionContext scoped to the test class. For example, an extension may offer a configuration option to switch between test method/class scope and need to signal that by returning a corresponding enum constant.

Moreover, should an extension support both test class and test method scope, e.g. by returning DEFAULT, it might need to know which is actually used. While it may be possible to determine the scope by inspecting the supplied ExtensionContext, it would be preferable to avoid the guess work.

Deliverables

  • Introduce ExtensionContextScope.TEST_CLASS
  • Introduce ExtensionContext.getScope() or similar
  • Change default scope to TEST_METHOD

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions