diff --git a/documentation/src/docs/asciidoc/link-attributes.adoc b/documentation/src/docs/asciidoc/link-attributes.adoc index 90a80a566f55..5c55bec09cf7 100644 --- a/documentation/src/docs/asciidoc/link-attributes.adoc +++ b/documentation/src/docs/asciidoc/link-attributes.adoc @@ -139,7 +139,6 @@ endif::[] :BeforeAllCallback: {javadoc-root}/org.junit.jupiter.api/org/junit/jupiter/api/extension/BeforeAllCallback.html[BeforeAllCallback] :BeforeEachCallback: {javadoc-root}/org.junit.jupiter.api/org/junit/jupiter/api/extension/BeforeEachCallback.html[BeforeEachCallback] :BeforeTestExecutionCallback: {javadoc-root}/org.junit.jupiter.api/org/junit/jupiter/api/extension/BeforeTestExecutionCallback.html[BeforeTestExecutionCallback] -:EnableTestScopedConstructorContext: {javadoc-root}/org.junit.jupiter.api/org/junit/jupiter/api/extension/EnableTestScopedConstructorContext.html[@EnableTestScopedConstructorContext] :ExecutableInvoker: {javadoc-root}/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExecutableInvoker.html[ExecutableInvoker] :ExecutionCondition: {javadoc-root}/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExecutionCondition.html[ExecutionCondition] :ExtendWith: {javadoc-root}/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExtendWith.html[@ExtendWith] diff --git a/documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc b/documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc index 08a328c14cd3..ba8710106188 100644 --- a/documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc +++ b/documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc @@ -58,10 +58,11 @@ JUnit repository on GitHub. extensions. * Allow determining "shared resources" at runtime via the new `@ResourceLock#providers` attribute that accepts implementations of `ResourceLocksProvider`. -* `@EnableTestScopedConstructorContext` has been added to enable the use of a test-scoped - `ExtensionContext` while instantiating the test instance. - The behavior enabled by the annotation is expected to eventually become the default in - future versions of JUnit Jupiter. +* Extensions that implement `TestInstancePreConstructCallback`, `TestInstanceFactory`, + `TestInstancePostProcessor`, `ParameterResolver`, or `InvocationInterceptor` may + override the `getTestInstantiationExtensionContextScope()` method to enable receiving + a test-scoped `ExtensionContext` in `Extension` methods called during test class + instantiation. This behavior will become the default in future versions of JUnit. * `@TempDir` is now supported on test class constructors. diff --git a/documentation/src/docs/asciidoc/user-guide/extensions.adoc b/documentation/src/docs/asciidoc/user-guide/extensions.adoc index 12f8b6ad54ad..b651f4506ae5 100644 --- a/documentation/src/docs/asciidoc/user-guide/extensions.adoc +++ b/documentation/src/docs/asciidoc/user-guide/extensions.adoc @@ -383,8 +383,9 @@ instances and their lifecycle. [NOTE] ==== -You may annotate your extension with `{EnableTestScopedConstructorContext}` for revised -handling of `CloseableResource` and to make test-specific data available to your implementation. +You may override the `getTestInstantiationExtensionContextScope(...)` method to return +`TEST_SCOPED` for revised handling of `CloseableResource` and to make test-specific data +available to your implementation. ==== [[extensions-test-instance-factories]] @@ -415,8 +416,9 @@ registered for any specific test class. [NOTE] ==== -You may annotate your extension with `{EnableTestScopedConstructorContext}` for revised -handling of `CloseableResource` and to make test-specific data available to your implementation. +You may override the `getTestInstantiationExtensionContextScope(...)` method to return +`TEST_SCOPED` for revised handling of `CloseableResource` and to make test-specific data +available to your implementation. ==== [[extensions-test-instance-post-processing]] @@ -433,8 +435,9 @@ For a concrete example, consult the source code for the `{MockitoExtension}` and [NOTE] ==== -You may annotate your extension with `{EnableTestScopedConstructorContext}` for revised -handling of `CloseableResource` and to make test-specific data available to your implementation. +You may override the `getTestInstantiationExtensionContextScope(...)` method to return +`TEST_SCOPED` for revised handling of `CloseableResource` and to make test-specific data +available to your implementation. ==== [[extensions-test-instance-pre-destroy-callback]] @@ -485,10 +488,11 @@ those provided in `java.lang.reflect.Parameter` in order to avoid this bug in th [NOTE] ==== -You may annotate your extension with `{EnableTestScopedConstructorContext}` to support -injecting test specific data into constructor parameters of the test instance. -The annotation makes JUnit use a test-specific `ExtensionContext` while resolving -constructor parameters, unless the lifecycle is set to `TestInstance.Lifecycle.PER_CLASS`. +You may override the `getTestInstantiationExtensionContextScope(...)` method to return +`TEST_SCOPED` to support injecting test specific data into constructor parameters of the +test class instance. Doing so causes a test-specific `{ExtensionContext}` to be used while +resolving constructor parameters, unless the +<> is set to `PER_CLASS`. ==== [NOTE] @@ -723,9 +727,9 @@ include::{testDir}/example/interceptor/SwingEdtInterceptor.java[tags=user_guide] [NOTE] ==== -You may annotate your extension with `{EnableTestScopedConstructorContext}` to make -test-specific data available to your implementation of `interceptTestClassConstructor` and -for a revised scope of the provided `Store` instance. +You may override the `getTestInstantiationExtensionContextScope(...)` method to return +`TEST_SCOPED` to make test-specific data available to your implementation of +`interceptTestClassConstructor` and for a revised scope of the provided `Store` instance. ==== [[extensions-test-templates]] diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/EnableTestScopedConstructorContext.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/EnableTestScopedConstructorContext.java deleted file mode 100644 index 58a3cc65ce5a..000000000000 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/EnableTestScopedConstructorContext.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2015-2024 the original author or authors. - * - * All rights reserved. This program and the accompanying materials are - * made available under the terms of the Eclipse Public License v2.0 which - * accompanies this distribution and is available at - * - * https://www.eclipse.org/legal/epl-v20.html - */ - -package org.junit.jupiter.api.extension; - -import static org.apiguardian.api.API.Status.MAINTAINED; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.apiguardian.api.API; -import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.extension.ExtensionContext.Store; -import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource; - -/** - * {@code @EnableTestScopedConstructorContext} allows - * {@link Extension Extensions} to use a test-scoped {@link ExtensionContext} - * during creation of test instances. - * - *

The annotation should be used on extension classes. - * JUnit will call the following extension callbacks of annotated extensions - * with a test-scoped {@link ExtensionContext}, unless the test class is - * annotated with {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. - * - *

- * - *

Implementations of these extension callbacks can observe the following - * differences if they are using {@code @EnableTestScopedConstructorContext}. - * - *

- * - *

Note: The behavior which is enabled by this annotation is - * expected to become the default in future versions of JUnit Jupiter. To ensure - * future compatibility, extension vendors are therefore advised to annotate - * their extensions, even if they don't need the new functionality. - * - * @since 5.12 - * @see InvocationInterceptor - * @see ParameterResolver - * @see TestInstancePreConstructCallback - * @see TestInstancePostProcessor - * @see TestInstanceFactory - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Inherited -@API(status = MAINTAINED, since = "5.12") -public @interface EnableTestScopedConstructorContext { -} diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/InvocationInterceptor.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/InvocationInterceptor.java index c13e6ed841d1..58aa743cd27a 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/InvocationInterceptor.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/InvocationInterceptor.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtensionContext.Store; /** * {@code InvocationInterceptor} defines the API for {@link Extension @@ -50,7 +51,7 @@ * @see ExtensionContext */ @API(status = STABLE, since = "5.10") -public interface InvocationInterceptor extends Extension { +public interface InvocationInterceptor extends TestInstantiationAwareExtension { /** * Intercept the invocation of a test class constructor. @@ -58,9 +59,10 @@ public interface InvocationInterceptor extends Extension { *

Note that the test class may not have been initialized * (static initialization) when this method is invoked. * - *

You may annotate your extension with {@link EnableTestScopedConstructorContext} - * to make test-specific data available to your implementation of this method and - * for a revised scope of the provided `Store` instance. + *

Extensions may override + * {@link #getTestInstantiationExtensionContextScope} to + * make test-specific data available to the implementation of this method + * and for a revised scope of the provided {@link Store Store} instance. * * @param invocation the invocation that is being intercepted; never * {@code null} diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ParameterResolver.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ParameterResolver.java index 74ad7feaae61..a0274c8558ab 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ParameterResolver.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ParameterResolver.java @@ -31,11 +31,14 @@ * an argument for the parameter must be resolved at runtime by a * {@code ParameterResolver}. * - *

You may annotate your extension with {@link EnableTestScopedConstructorContext} - * to support injecting test specific data into constructor parameters of the test instance. - * The annotation makes JUnit use a test-specific `ExtensionContext` while resolving - * constructor parameters, unless the test class is annotated with - * {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. + *

Extensions may override + * {@link #getTestInstantiationExtensionContextScope} to + * support injecting test specific data into constructor parameters of the test + * class instance. Returning + * {@link ExtensionContextScope#TEST_METHOD TEST_SCOPED} from this method, + * causes a test-specific {@link ExtensionContext} to be used while resolving + * constructor parameters, unless the lifecycle is set to + * {@link TestInstance.Lifecycle#PER_CLASS PER_CLASS}. * *

Constructor Requirements

* @@ -51,7 +54,7 @@ * @see TestInstancePreDestroyCallback */ @API(status = STABLE, since = "5.0") -public interface ParameterResolver extends Extension { +public interface ParameterResolver extends TestInstantiationAwareExtension { /** * Determine if this resolver supports resolution of an argument for the diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstanceFactory.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstanceFactory.java index f341e88e3e2d..4b9880dc79ba 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstanceFactory.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstanceFactory.java @@ -52,15 +52,15 @@ */ @FunctionalInterface @API(status = STABLE, since = "5.7") -public interface TestInstanceFactory extends Extension { +public interface TestInstanceFactory extends TestInstantiationAwareExtension { /** * Callback for creating a test instance for the supplied context. * - *

You may annotate your extension with - * {@link EnableTestScopedConstructorContext @EnableTestScopedConstructorContext} - * for revised handling of {@link CloseableResource CloseableResource} and - * to make test-specific data available to your implementation. + *

Extensions may override + * {@link #getTestInstantiationExtensionContextScope} for + * revised handling of {@link CloseableResource CloseableResource} and to + * make test-specific data available to your implementation. * *

Note: the {@code ExtensionContext} supplied to a * {@code TestInstanceFactory} will always return an empty diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePostProcessor.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePostProcessor.java index a1aa465c5737..9e80175cffb4 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePostProcessor.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePostProcessor.java @@ -41,15 +41,15 @@ */ @FunctionalInterface @API(status = STABLE, since = "5.0") -public interface TestInstancePostProcessor extends Extension { +public interface TestInstancePostProcessor extends TestInstantiationAwareExtension { /** * Callback for post-processing the supplied test instance. * - *

You may annotate your extension with - * {@link EnableTestScopedConstructorContext @EnableTestScopedConstructorContext} - * for revised handling of {@link CloseableResource CloseableResource} and - * to make test-specific data available to your implementation. + *

Extensions may override + * {@link #getTestInstantiationExtensionContextScope} for + * revised handling of {@link CloseableResource CloseableResource} and to + * make test-specific data available to your implementation. * *

Note: the {@code ExtensionContext} supplied to a * {@code TestInstancePostProcessor} will always return an empty diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePreConstructCallback.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePreConstructCallback.java index b627c52f9132..774b9a375a09 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePreConstructCallback.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePreConstructCallback.java @@ -14,7 +14,6 @@ import org.apiguardian.api.API; import org.junit.jupiter.api.TestInstance.Lifecycle; -import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource; /** * {@code TestInstancePreConstructCallback} defines the API for {@link Extension @@ -45,15 +44,16 @@ */ @FunctionalInterface @API(status = STABLE, since = "5.11") -public interface TestInstancePreConstructCallback extends Extension { +public interface TestInstancePreConstructCallback extends TestInstantiationAwareExtension { /** * Callback invoked prior to test instances being constructed. * - *

You may annotate your extension with - * {@link EnableTestScopedConstructorContext @EnableTestScopedConstructorContext} - * for revised handling of {@link CloseableResource CloseableResource} and - * to make test-specific data available to your implementation. + *

Extensions may override + * {@link #getTestInstantiationExtensionContextScope} to + * make test-specific data available to the implementation of this method + * and for a revised scope of the provided + * {@link ExtensionContext.Store Store} instance. * * @param factoryContext the context for the test instance about to be instantiated; * never {@code null} diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstantiationAwareExtension.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstantiationAwareExtension.java new file mode 100644 index 000000000000..48c327705413 --- /dev/null +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstantiationAwareExtension.java @@ -0,0 +1,126 @@ +/* + * Copyright 2015-2024 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ + +package org.junit.jupiter.api.extension; + +import static org.apiguardian.api.API.Status.DEPRECATED; +import static org.apiguardian.api.API.Status.EXPERIMENTAL; + +import org.apiguardian.api.API; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.ExtensionContext.Store; +import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource; + +/** + * Interface for {@link Extension Extensions} that are aware and can influence + * the instantiation of test instances. + * + *

This interface is not indented to be implemented directly. Instead, extend + * one of its sub-interfaces. + * + * @since 5.12 + * @see InvocationInterceptor#interceptTestClassConstructor + * @see ParameterResolver + * @see TestInstancePreConstructCallback + * @see TestInstancePostProcessor + * @see TestInstanceFactory + */ +@API(status = EXPERIMENTAL, since = "5.12") +public interface TestInstantiationAwareExtension extends Extension { + + /** + * Whether this extension should receive a test-scoped + * {@link ExtensionContext} during the instantiation of test instances. + * + *

If an extension returns + * {@link ExtensionContextScope#TEST_METHOD TEST_METHOD} from this method, + * the following extension methods will be called with a test-scoped + * {@link ExtensionContext} instead of a class-scoped one, unless the + * {@link TestInstance.Lifecycle#PER_CLASS PER_CLASS} lifecycle is used: + * + *

+ * + *

In such cases, implementations of these extension callbacks can + * observe the following differences: + * + *

+ * + *

Note: The behavior which is enabled by returning + * {@link ExtensionContextScope#TEST_METHOD TEST_METHOD} from this method + * will become the default in future versions of JUnit. To ensure forward + * compatibility, extension implementors are therefore advised to opt in, + * even if they don't require the new functionality. + * + * @implNote There are no guarantees about how often this method is called. + * Therefore, implementations should be idempotent and avoid side + * effects. They may, however, cache the result for performance in + * the {@link Store Store} of the supplied + * {@link ExtensionContext}, if necessary. + * @param rootContext the root extension context to allow inspection of + * configuration parameters; never {@code null} + * @since 5.12 + */ + @API(status = EXPERIMENTAL, since = "5.12") + default ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return ExtensionContextScope.DEFAULT; + } + + /** + * {@code ExtensionContextScope} is used to define the scope of the + * {@link ExtensionContext} passed to an extension during the instantiation + * of test instances. + * + * @since 5.12 + * @see TestInstantiationAwareExtension#getTestInstantiationExtensionContextScope + */ + @API(status = EXPERIMENTAL, since = "5.12") + enum ExtensionContextScope { + + /** + * The extension should receive an {@link ExtensionContext} scoped to + * the test class. + * + * @deprecated This behavior will be removed from future versions of + * JUnit and {@link #TEST_METHOD} will become the default. + */ + @API(status = DEPRECATED, since = "5.12") // + @Deprecated + DEFAULT, + + /** + * The extension should receive an {@link ExtensionContext} scoped to + * the test method, unless the + * {@link TestInstance.Lifecycle#PER_CLASS PER_CLASS} lifecycle is used. + */ + TEST_METHOD + } + +} diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/ExtensionContextSupplier.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/ExtensionContextSupplier.java index 1a81c0df6536..bc42951196cf 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/ExtensionContextSupplier.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/ExtensionContextSupplier.java @@ -11,18 +11,18 @@ package org.junit.jupiter.engine.execution; import static org.apiguardian.api.API.Status.INTERNAL; +import static org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope.TEST_METHOD; import org.apiguardian.api.API; -import org.junit.jupiter.api.extension.EnableTestScopedConstructorContext; -import org.junit.jupiter.api.extension.Extension; import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.platform.commons.util.AnnotationUtils; +import org.junit.jupiter.api.extension.TestInstantiationAwareExtension; /** * Container of two instances of {@link ExtensionContext} to simplify the legacy for - * #3445 (Introduction of Test-scoped ExtensionContext). + * #3445. * * @since 5.12 + * @see TestInstantiationAwareExtension */ @API(status = INTERNAL, since = "5.12") public final class ExtensionContextSupplier { @@ -35,13 +35,17 @@ public ExtensionContextSupplier(ExtensionContext currentExtensionContext, Extens this.legacyExtensionContext = legacyExtensionContext; } - public ExtensionContext get(Extension extension) { - if (currentExtensionContext == legacyExtensionContext - || AnnotationUtils.isAnnotated(extension.getClass(), EnableTestScopedConstructorContext.class)) { + public ExtensionContext get(TestInstantiationAwareExtension extension) { + if (currentExtensionContext == legacyExtensionContext || isTestScoped(extension)) { return currentExtensionContext; } else { return legacyExtensionContext; } } + + private boolean isTestScoped(TestInstantiationAwareExtension extension) { + ExtensionContext rootContext = currentExtensionContext.getRoot(); + return extension.getTestInstantiationExtensionContextScope(rootContext) == TEST_METHOD; + } } diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java index ff6484b960bb..c72802b9de24 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java @@ -12,6 +12,7 @@ import static java.nio.file.FileVisitResult.CONTINUE; import static java.util.stream.Collectors.joining; +import static org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope.TEST_METHOD; import static org.junit.jupiter.api.io.CleanupMode.DEFAULT; import static org.junit.jupiter.api.io.CleanupMode.NEVER; import static org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS; @@ -44,7 +45,6 @@ import org.junit.jupiter.api.extension.AnnotatedElementContext; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeEachCallback; -import org.junit.jupiter.api.extension.EnableTestScopedConstructorContext; import org.junit.jupiter.api.extension.ExtensionConfigurationException; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext.Namespace; @@ -77,7 +77,6 @@ * @see TempDir * @see Files#createTempDirectory */ -@EnableTestScopedConstructorContext class TempDirectory implements BeforeAllCallback, BeforeEachCallback, ParameterResolver { static final Namespace NAMESPACE = Namespace.create(TempDirectory.class); @@ -94,6 +93,11 @@ public TempDirectory(JupiterConfiguration configuration) { this.configuration = configuration; } + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return TEST_METHOD; + } + /** * Perform field injection for non-private, {@code static} fields (i.e., * class fields) of type {@link Path} or {@link File} that are annotated with diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/extension/KitchenSinkExtension.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/extension/KitchenSinkExtension.java index 47990d9bdce0..762c0414c3e3 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/extension/KitchenSinkExtension.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/extension/KitchenSinkExtension.java @@ -65,6 +65,13 @@ public class KitchenSinkExtension implements // @formatter:on { + // --- Test Class Instantiation -------------------------------------------- + + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return ExtensionContextScope.TEST_METHOD; + } + // --- Lifecycle Callbacks ------------------------------------------------- @Override diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstanceFactoryTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstanceFactoryTests.java index 48d4218faccd..208dfc863a4a 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstanceFactoryTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstanceFactoryTests.java @@ -13,6 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; +import static org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope.TEST_METHOD; import static org.junit.platform.commons.util.ClassUtils.nullSafeToString; import static org.junit.platform.testkit.engine.EventConditions.container; import static org.junit.platform.testkit.engine.EventConditions.engine; @@ -35,7 +36,6 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.extension.EnableTestScopedConstructorContext; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionConfigurationException; import org.junit.jupiter.api.extension.ExtensionContext; @@ -706,12 +706,18 @@ public Object createTestInstance(TestInstanceFactoryContext factoryContext, Exte } } - @EnableTestScopedConstructorContext private static class FooInstanceFactory extends AbstractTestInstanceFactory { + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return TEST_METHOD; + } } - @EnableTestScopedConstructorContext private static class BarInstanceFactory extends AbstractTestInstanceFactory { + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return TEST_METHOD; + } } private static class LegacyInstanceFactory extends AbstractTestInstanceFactory { diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java index 416e90e8e532..370a5f7848c8 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java @@ -12,6 +12,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope.TEST_METHOD; import java.util.ArrayList; import java.util.HashMap; @@ -21,7 +22,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.EnableTestScopedConstructorContext; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.TestInstancePostProcessor; @@ -198,18 +198,26 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex } } - @EnableTestScopedConstructorContext static class FooInstancePostProcessor extends AbstractInstancePostProcessor { FooInstancePostProcessor() { super("foo"); } + + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return TEST_METHOD; + } } - @EnableTestScopedConstructorContext static class BarInstancePostProcessor extends AbstractInstancePostProcessor { BarInstancePostProcessor() { super("bar"); } + + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return TEST_METHOD; + } } static class LegacyInstancePostProcessor extends AbstractInstancePostProcessor { diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstancePreConstructCallbackTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstancePreConstructCallbackTests.java index 4da0833bd0d7..d099c7b4d1fe 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstancePreConstructCallbackTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TestInstancePreConstructCallbackTests.java @@ -11,6 +11,7 @@ package org.junit.jupiter.engine.extension; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope.TEST_METHOD; import java.util.ArrayList; import java.util.List; @@ -23,7 +24,6 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.extension.EnableTestScopedConstructorContext; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.RegisterExtension; @@ -552,25 +552,37 @@ else if (context.getTestInstanceLifecycle().orElse(null) != TestInstance.Lifecyc } } - @EnableTestScopedConstructorContext static class InstancePreConstructCallbackRecordingFoo extends AbstractTestInstancePreConstructCallback { InstancePreConstructCallbackRecordingFoo() { super("foo"); } + + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return TEST_METHOD; + } } - @EnableTestScopedConstructorContext static class InstancePreConstructCallbackRecordingBar extends AbstractTestInstancePreConstructCallback { InstancePreConstructCallbackRecordingBar() { super("bar"); } + + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return TEST_METHOD; + } } - @EnableTestScopedConstructorContext static class InstancePreConstructCallbackRecordingBaz extends AbstractTestInstancePreConstructCallback { InstancePreConstructCallbackRecordingBaz() { super("baz"); } + + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return TEST_METHOD; + } } static class InstancePreConstructCallbackRecordingLegacy extends AbstractTestInstancePreConstructCallback {