diff --git a/documentation/src/docs/asciidoc/release-notes-5.0.0-M5.adoc b/documentation/src/docs/asciidoc/release-notes-5.0.0-M5.adoc index cc297d28e969..1da981872e96 100644 --- a/documentation/src/docs/asciidoc/release-notes-5.0.0-M5.adoc +++ b/documentation/src/docs/asciidoc/release-notes-5.0.0-M5.adoc @@ -128,10 +128,6 @@ is placed on the Java 9 module path. * `TestExecutionCondition` and `ContainerExecutionCondition` have been replaced by a single, general purpose extension API for conditional test execution: `ExecutionCondition`. -* The `postProcessTestInstance()` method in the `TestInstancePostProcessor` API is no - longer supplied a direct reference to the test instance. However, the test instance - can now be retrieved from the supplied `ExtensionContext` via its - `Optional getTestInstance()` method. [[release-notes-5.0.0-m5-migration-extension-api]] .Extension API Migration diff --git a/documentation/src/test/java/example/TestInfoDemo.java b/documentation/src/test/java/example/TestInfoDemo.java index 8b7bdf7c223b..5a2ef810c152 100644 --- a/documentation/src/test/java/example/TestInfoDemo.java +++ b/documentation/src/test/java/example/TestInfoDemo.java @@ -24,8 +24,7 @@ class TestInfoDemo { TestInfoDemo(TestInfo testInfo) { - String displayName = testInfo.getDisplayName(); - assertTrue(displayName.equals("TEST 1") || displayName.equals("test2()")); + assertEquals("TestInfo Demo", testInfo.getDisplayName()); } @BeforeEach 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 85404c35420c..5249743f3424 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 @@ -25,18 +25,16 @@ *

Implementations must provide a no-args constructor. * * @since 5.0 - * @see #postProcessTestInstance(ExtensionContext) */ @API(Experimental) public interface TestInstancePostProcessor extends Extension { /** - * Callback for post-processing the test instance in the supplied - * {@link ExtensionContext}. + * Callback for post-processing the supplied test instance. * + * @param testInstance the instance to post-process; never {@code null} * @param context the current extension context; never {@code null} - * @see ExtensionContext#getTestInstance() */ - void postProcessTestInstance(ExtensionContext context) throws Exception; + void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception; } diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/AbstractExtensionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/AbstractExtensionContext.java similarity index 73% rename from junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/AbstractExtensionContext.java rename to junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/AbstractExtensionContext.java index 37f6202bffd7..6f492eed4be7 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/AbstractExtensionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/AbstractExtensionContext.java @@ -8,10 +8,9 @@ * http://www.eclipse.org/legal/epl-v10.html */ -package org.junit.jupiter.engine.execution; +package org.junit.jupiter.engine.descriptor; import static java.util.stream.Collectors.toCollection; -import static org.junit.platform.commons.meta.API.Usage.Internal; import java.util.LinkedHashSet; import java.util.Map; @@ -19,7 +18,8 @@ import java.util.Set; import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.platform.commons.meta.API; +import org.junit.jupiter.engine.execution.ExtensionValuesStore; +import org.junit.jupiter.engine.execution.NamespaceAwareStore; import org.junit.platform.commons.util.Preconditions; import org.junit.platform.engine.EngineExecutionListener; import org.junit.platform.engine.TestDescriptor; @@ -29,28 +29,25 @@ /** * @since 5.0 */ -@API(Internal) -public abstract class AbstractExtensionContext implements ExtensionContext { +abstract class AbstractExtensionContext implements ExtensionContext { private final ExtensionContext parent; private final EngineExecutionListener engineExecutionListener; private final T testDescriptor; private final ExtensionValuesStore valuesStore; - private Object testInstance; - - protected AbstractExtensionContext(AbstractExtensionContext parent, - EngineExecutionListener engineExecutionListener, T testDescriptor) { + AbstractExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener, + T testDescriptor) { this.parent = parent; this.engineExecutionListener = engineExecutionListener; this.testDescriptor = testDescriptor; this.valuesStore = createStore(parent); } - private ExtensionValuesStore createStore(AbstractExtensionContext parent) { + private ExtensionValuesStore createStore(ExtensionContext parent) { ExtensionValuesStore parentStore = null; if (parent != null) { - parentStore = parent.valuesStore; + parentStore = ((AbstractExtensionContext) parent).valuesStore; } return new ExtensionValuesStore(parentStore); } @@ -65,15 +62,6 @@ public String getDisplayName() { return getTestDescriptor().getDisplayName(); } - public void setTestInstance(Object testInstance) { - this.testInstance = testInstance; - } - - @Override - public Optional getTestInstance() { - return Optional.ofNullable(this.testInstance); - } - @Override public void publishReportEntry(Map values) { engineExecutionListener.reportingEntryPublished(this.testDescriptor, ReportEntry.from(values)); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassExtensionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassExtensionContext.java index aa94347268b9..9753154f5cef 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassExtensionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassExtensionContext.java @@ -16,7 +16,7 @@ import java.lang.reflect.Method; import java.util.Optional; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.engine.execution.ThrowableCollector; import org.junit.platform.commons.meta.API; import org.junit.platform.engine.EngineExecutionListener; @@ -28,8 +28,9 @@ public final class ClassExtensionContext extends AbstractExtensionContext { private final ThrowableCollector throwableCollector; + private Object testInstance; - public ClassExtensionContext(AbstractExtensionContext parent, EngineExecutionListener engineExecutionListener, + public ClassExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener, ClassTestDescriptor testDescriptor, ThrowableCollector throwableCollector) { super(parent, engineExecutionListener, testDescriptor); @@ -46,6 +47,15 @@ public Optional> getTestClass() { return Optional.of(getTestDescriptor().getTestClass()); } + void setTestInstance(Object testInstance) { + this.testInstance = testInstance; + } + + @Override + public Optional getTestInstance() { + return Optional.ofNullable(this.testInstance); + } + @Override public Optional getTestMethod() { return Optional.empty(); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java index a794afe87e4d..aea364e4d16c 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.function.Consumer; import java.util.function.Function; import org.junit.jupiter.api.TestInstance; @@ -31,7 +32,6 @@ import org.junit.jupiter.api.extension.Extension; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.TestInstancePostProcessor; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; import org.junit.jupiter.engine.execution.AfterEachMethodAdapter; import org.junit.jupiter.engine.execution.BeforeEachMethodAdapter; import org.junit.jupiter.engine.execution.ExecutableInvoker; @@ -176,50 +176,39 @@ private TestInstanceProvider testInstanceProvider(JupiterEngineExecutionContext if (this.lifecycle == Lifecycle.PER_CLASS) { // Eagerly load test instance for BeforeAllCallbacks, if necessary, // and store the instance in the ExtensionContext. - // Note: as a side effect, instantiateAndPostProcessTestInstance() also stores - // the instance it creates in the "current" extension context. - Object instance = instantiateAndPostProcessTestInstance(parentExecutionContext, extensionContext, registry); - - // Return a TestInstanceProvider that additionally sets the test instance - // in the supplied child extension context (e.g., a MethodExtensionContext). - return (childContext, childRegistry) -> { - childContext.setTestInstance(instance); - return instance; - }; + Object instance = instantiateAndPostProcessTestInstance(parentExecutionContext, extensionContext, registry, + extensionContext::setTestInstance); + return childRegistry -> instance; } // else Lifecycle.PER_METHOD - return (childContext, childRegistry) -> instantiateAndPostProcessTestInstance(parentExecutionContext, - childContext, childRegistry.orElse(registry)); + return childRegistry -> instantiateAndPostProcessTestInstance(parentExecutionContext, extensionContext, + childRegistry.orElse(registry), instance -> { + // no extension context update required + }); } - /** - * Instantiate the test instance; set the test instance in the supplied - * extension context; and post process the test instance. - * - * @see #instantiateTestClass - * @see AbstractExtensionContext#setTestInstance - * @see #invokeTestInstancePostProcessors - */ private Object instantiateAndPostProcessTestInstance(JupiterEngineExecutionContext context, - AbstractExtensionContext extensionContext, ExtensionRegistry registry) { + ExtensionContext extensionContext, ExtensionRegistry registry, Consumer testInstanceConsumer) { Object instance = instantiateTestClass(context, registry, extensionContext); - extensionContext.setTestInstance(instance); - invokeTestInstancePostProcessors(registry, extensionContext); + testInstanceConsumer.accept(instance); + invokeTestInstancePostProcessors(instance, registry, extensionContext); return instance; } protected Object instantiateTestClass(JupiterEngineExecutionContext parentExecutionContext, - ExtensionRegistry registry, AbstractExtensionContext extensionContext) { + ExtensionRegistry registry, ExtensionContext extensionContext) { Constructor constructor = ReflectionUtils.getDeclaredConstructor(this.testClass); return executableInvoker.invoke(constructor, extensionContext, registry); } - private void invokeTestInstancePostProcessors(ExtensionRegistry registry, ExtensionContext context) { + private void invokeTestInstancePostProcessors(Object instance, ExtensionRegistry registry, + ExtensionContext context) { + registry.stream(TestInstancePostProcessor.class).forEach( - extension -> executeAndMaskThrowable(() -> extension.postProcessTestInstance(context))); + extension -> executeAndMaskThrowable(() -> extension.postProcessTestInstance(instance, context))); } private void invokeBeforeAllCallbacks(JupiterEngineExecutionContext context) { @@ -300,6 +289,7 @@ private AfterEachMethodAdapter synthesizeAfterEachMethodAdapter(Method method) { } private void invokeMethodInExtensionContext(Method method, ExtensionContext context, ExtensionRegistry registry) { + Object testInstance = context.getTestInstance().orElseThrow(() -> new JUnitException( "Illegal state: test instance not present for method: " + method.toGenericString())); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineDescriptor.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineDescriptor.java index f50b4fc305ab..6f13c3620a78 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineDescriptor.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineDescriptor.java @@ -13,7 +13,7 @@ import static org.junit.jupiter.engine.extension.ExtensionRegistry.createRegistryWithDefaultExtensions; import static org.junit.platform.commons.meta.API.Usage.Internal; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; import org.junit.jupiter.engine.extension.ExtensionRegistry; import org.junit.platform.commons.meta.API; @@ -36,7 +36,7 @@ public JupiterEngineDescriptor(UniqueId uniqueId) { public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) throws Exception { ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(context.getConfigurationParameters()); EngineExecutionListener executionListener = context.getExecutionListener(); - AbstractExtensionContext extensionContext = new JupiterEngineExtensionContext(executionListener, this); + ExtensionContext extensionContext = new JupiterEngineExtensionContext(executionListener, this); // @formatter:off return context.extend() diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineExtensionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineExtensionContext.java index 908d4382f14c..b1c5f307bd4f 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineExtensionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineExtensionContext.java @@ -16,7 +16,6 @@ import java.lang.reflect.Method; import java.util.Optional; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; import org.junit.platform.commons.meta.API; import org.junit.platform.engine.EngineExecutionListener; @@ -41,6 +40,11 @@ public Optional> getTestClass() { return Optional.empty(); } + @Override + public Optional getTestInstance() { + return Optional.empty(); + } + @Override public Optional getTestMethod() { return Optional.empty(); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodExtensionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodExtensionContext.java index 2ba94b2cca91..764ddda47f56 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodExtensionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodExtensionContext.java @@ -16,7 +16,7 @@ import java.lang.reflect.Method; import java.util.Optional; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.engine.execution.ThrowableCollector; import org.junit.platform.commons.meta.API; import org.junit.platform.engine.EngineExecutionListener; @@ -27,13 +27,16 @@ @API(Internal) public final class MethodExtensionContext extends AbstractExtensionContext { + private final Object testInstance; + private final ThrowableCollector throwableCollector; - public MethodExtensionContext(AbstractExtensionContext parent, EngineExecutionListener engineExecutionListener, - MethodTestDescriptor testDescriptor, ThrowableCollector throwableCollector) { + public MethodExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener, + MethodTestDescriptor testDescriptor, Object testInstance, ThrowableCollector throwableCollector) { super(parent, engineExecutionListener, testDescriptor); + this.testInstance = testInstance; this.throwableCollector = throwableCollector; } @@ -52,6 +55,11 @@ public Optional getTestMethod() { return Optional.of(getTestDescriptor().getTestMethod()); } + @Override + public Optional getTestInstance() { + return Optional.of(this.testInstance); + } + @Override public Optional getExecutionException() { return Optional.ofNullable(this.throwableCollector.getThrowable()); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodTestDescriptor.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodTestDescriptor.java index 887c9a98b3d6..8dd00f3bba48 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodTestDescriptor.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodTestDescriptor.java @@ -25,7 +25,6 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; import org.junit.jupiter.api.function.Executable; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; import org.junit.jupiter.engine.execution.AfterEachMethodAdapter; import org.junit.jupiter.engine.execution.BeforeEachMethodAdapter; import org.junit.jupiter.engine.execution.ExecutableInvoker; @@ -79,15 +78,10 @@ public Type getType() { @Override public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) throws Exception { ExtensionRegistry registry = populateNewExtensionRegistry(context); + Object testInstance = context.getTestInstanceProvider().getTestInstance(Optional.of(registry)); ThrowableCollector throwableCollector = new ThrowableCollector(); - AbstractExtensionContext extensionContext = new MethodExtensionContext(context.getExtensionContext(), - context.getExecutionListener(), this, throwableCollector); - - // Even though we (intentionally) ignore the return value, the following line - // is required since the configured TestInstanceProvider is responsible for - // setting the test instance in the supplied extension context. - // See ClassTestDescriptor#testInstanceProvider(...) for details. - context.getTestInstanceProvider().getTestInstance(extensionContext, Optional.of(registry)); + ExtensionContext extensionContext = new MethodExtensionContext(context.getExtensionContext(), + context.getExecutionListener(), this, testInstance, throwableCollector); // @formatter:off return context.extend() @@ -105,7 +99,6 @@ protected ExtensionRegistry populateNewExtensionRegistry(JupiterEngineExecutionC @Override public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) throws Exception { - ThrowableCollector throwableCollector = context.getThrowableCollector(); // @formatter:off diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/NestedClassTestDescriptor.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/NestedClassTestDescriptor.java index 7a38e90b25d7..3de1c4f253db 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/NestedClassTestDescriptor.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/NestedClassTestDescriptor.java @@ -16,7 +16,7 @@ import java.util.Optional; import java.util.Set; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.engine.execution.ExecutableInvoker; import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; import org.junit.jupiter.engine.extension.ExtensionRegistry; @@ -58,11 +58,11 @@ public final Set getTags() { @Override protected Object instantiateTestClass(JupiterEngineExecutionContext parentExecutionContext, - ExtensionRegistry registry, AbstractExtensionContext extensionContext) { + ExtensionRegistry registry, ExtensionContext extensionContext) { // Extensions registered for nested classes and below are not to be used for instantiating outer classes Optional childExtensionRegistryForOuterInstance = Optional.empty(); - Object outerInstance = parentExecutionContext.getTestInstanceProvider().getTestInstance(extensionContext, + Object outerInstance = parentExecutionContext.getTestInstanceProvider().getTestInstance( childExtensionRegistryForOuterInstance); Constructor constructor = ReflectionUtils.getDeclaredConstructor(getTestClass()); return executableInvoker.invoke(constructor, outerInstance, extensionContext, registry); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateExtensionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateExtensionContext.java index ef59c48eeffe..6a03ed0f295a 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateExtensionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateExtensionContext.java @@ -16,7 +16,7 @@ import java.lang.reflect.Method; import java.util.Optional; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.platform.commons.meta.API; import org.junit.platform.engine.EngineExecutionListener; @@ -26,10 +26,13 @@ @API(Internal) final class TestTemplateExtensionContext extends AbstractExtensionContext { - TestTemplateExtensionContext(AbstractExtensionContext parent, EngineExecutionListener engineExecutionListener, - TestTemplateTestDescriptor testDescriptor) { + private final Object testInstance; + + TestTemplateExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener, + TestTemplateTestDescriptor testDescriptor, Object testInstance) { super(parent, engineExecutionListener, testDescriptor); + this.testInstance = testInstance; } @Override @@ -42,6 +45,11 @@ public Optional> getTestClass() { return Optional.of(getTestDescriptor().getTestClass()); } + @Override + public Optional getTestInstance() { + return Optional.ofNullable(this.testInstance); + } + @Override public Optional getTestMethod() { return Optional.of(getTestDescriptor().getTestMethod()); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java index 093fbf4d9fd7..493b5ebf91c7 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.TestTemplateInvocationContext; import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; import org.junit.jupiter.engine.extension.ExtensionRegistry; import org.junit.platform.commons.meta.API; @@ -59,13 +58,11 @@ public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext conte ExtensionRegistry registry = populateNewExtensionRegistryFromExtendWith(getTestMethod(), context.getExtensionRegistry()); - AbstractExtensionContext extensionContext = new TestTemplateExtensionContext(context.getExtensionContext(), - context.getExecutionListener(), this); + // The test instance should be properly maintained by the enclosing class's ExtensionContext. + Object testInstance = context.getExtensionContext().getTestInstance().orElse(null); - // We don't require a test instance here, but we are responsible for providing it - // to extensions if the enclosing class's ExtensionContext already contains one. - // See ClassTestDescriptor#testInstanceProvider(...) for details. - extensionContext.setTestInstance(context.getExtensionContext().getTestInstance().orElse(null)); + ExtensionContext extensionContext = new TestTemplateExtensionContext(context.getExtensionContext(), + context.getExecutionListener(), this, testInstance); // @formatter:off return context.extend() diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java index a306151af700..a32ea42e7f44 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java @@ -12,6 +12,7 @@ import static org.junit.platform.commons.meta.API.Usage.Internal; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.engine.extension.ExtensionRegistry; import org.junit.platform.commons.JUnitException; import org.junit.platform.commons.meta.API; @@ -55,7 +56,7 @@ public ExtensionRegistry getExtensionRegistry() { return this.state.extensionRegistry; } - public AbstractExtensionContext getExtensionContext() { + public ExtensionContext getExtensionContext() { return this.state.extensionContext; } @@ -81,7 +82,7 @@ private static final class State implements Cloneable { final ConfigurationParameters configurationParameters; TestInstanceProvider testInstanceProvider; ExtensionRegistry extensionRegistry; - AbstractExtensionContext extensionContext; + ExtensionContext extensionContext; ThrowableCollector throwableCollector; State(EngineExecutionListener executionListener, ConfigurationParameters configurationParameters) { @@ -120,7 +121,7 @@ public Builder withExtensionRegistry(ExtensionRegistry extensionRegistry) { return this; } - public Builder withExtensionContext(AbstractExtensionContext extensionContext) { + public Builder withExtensionContext(ExtensionContext extensionContext) { newState().extensionContext = extensionContext; return this; } diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/TestInstanceProvider.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/TestInstanceProvider.java index adad70b4b802..b73dc77d3da8 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/TestInstanceProvider.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/TestInstanceProvider.java @@ -24,7 +24,6 @@ @API(Internal) public interface TestInstanceProvider { - Object getTestInstance(AbstractExtensionContext childExtensionContext, - Optional childExtensionRegistry); + Object getTestInstance(Optional childExtensionRegistry); } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/KitchenSinkExtension.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/KitchenSinkExtension.java index e119dc9e121d..be470be16599 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/KitchenSinkExtension.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/KitchenSinkExtension.java @@ -85,7 +85,7 @@ public void afterAll(ExtensionContext context) throws Exception { // --- Dependency Injection ------------------------------------------------ @Override - public void postProcessTestInstance(ExtensionContext context) throws Exception { + public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception { } @Override @@ -118,6 +118,7 @@ public boolean supportsTestTemplate(ExtensionContext context) { @Override public Stream provideTestTemplateInvocationContexts(ExtensionContext context) { + return null; } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestInstanceLifecycleTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestInstanceLifecycleTests.java index 7d5740f36c18..0d7b0bed1100 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestInstanceLifecycleTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestInstanceLifecycleTests.java @@ -98,19 +98,17 @@ void instancePerMethod() { performAssertions(testClass, containers, tests, instances, nestedInstances, allMethods, eachMethods); String containerExecutionConditionKey = executionConditionKey(testClass, null); + String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass); String beforeAllCallbackKey = beforeAllCallbackKey(testClass); String afterAllCallbackKey = afterAllCallbackKey(testClass); String testTemplateKey = testTemplateKey(testClass, "singletonTest"); String testExecutionConditionKey1 = executionConditionKey(testClass, testsInvoked.get(0)); - String postProcessTestInstanceKey1 = postProcessTestInstanceKey(testClass, testClass, testsInvoked.get(0)); String beforeEachCallbackKey1 = beforeEachCallbackKey(testClass, testsInvoked.get(0)); String afterEachCallbackKey1 = afterEachCallbackKey(testClass, testsInvoked.get(0)); String testExecutionConditionKey2 = executionConditionKey(testClass, testsInvoked.get(1)); - String postProcessTestInstanceKey2 = postProcessTestInstanceKey(testClass, testClass, testsInvoked.get(1)); String beforeEachCallbackKey2 = beforeEachCallbackKey(testClass, testsInvoked.get(1)); String afterEachCallbackKey2 = afterEachCallbackKey(testClass, testsInvoked.get(1)); String testExecutionConditionKey3 = executionConditionKey(testClass, testsInvoked.get(2)); - String postProcessTestInstanceKey3 = postProcessTestInstanceKey(testClass, testClass, testsInvoked.get(2)); String beforeEachCallbackKey3 = beforeEachCallbackKey(testClass, testsInvoked.get(2)); String afterEachCallbackKey3 = afterEachCallbackKey(testClass, testsInvoked.get(2)); @@ -118,17 +116,15 @@ void instancePerMethod() { assertThat(instanceMap.keySet()).containsExactlyInAnyOrder( containerExecutionConditionKey, beforeAllCallbackKey, + postProcessTestInstanceKey, testTemplateKey, testExecutionConditionKey1, - postProcessTestInstanceKey1, beforeEachCallbackKey1, afterEachCallbackKey1, testExecutionConditionKey2, - postProcessTestInstanceKey2, beforeEachCallbackKey2, afterEachCallbackKey2, testExecutionConditionKey3, - postProcessTestInstanceKey3, beforeEachCallbackKey3, afterEachCallbackKey3, afterAllCallbackKey @@ -143,19 +139,17 @@ void instancePerMethod() { assertNotNull(instance); assertSame(instance, instanceMap.get(afterEachCallbackKey1)); assertSame(instance, instanceMap.get(testExecutionConditionKey1)); - assertSame(instance, instanceMap.get(postProcessTestInstanceKey1)); instance = instanceMap.get(beforeEachCallbackKey2); assertNotNull(instance); assertSame(instance, instanceMap.get(afterEachCallbackKey2)); assertSame(instance, instanceMap.get(testExecutionConditionKey2)); - assertSame(instance, instanceMap.get(postProcessTestInstanceKey2)); instance = instanceMap.get(beforeEachCallbackKey3); assertNotNull(instance); assertSame(instance, instanceMap.get(afterEachCallbackKey3)); assertSame(instance, instanceMap.get(testExecutionConditionKey3)); - assertSame(instance, instanceMap.get(postProcessTestInstanceKey3)); + assertSame(instance, instanceMap.get(postProcessTestInstanceKey)); } @Test @@ -180,7 +174,7 @@ private void instancePerClass(Class testClass) { String containerExecutionConditionKey = executionConditionKey(testClass, null); String testTemplateKey = testTemplateKey(testClass, "singletonTest"); - String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass, testClass, null); + String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass); String beforeAllCallbackKey = beforeAllCallbackKey(testClass); String afterAllCallbackKey = afterAllCallbackKey(testClass); String testExecutionConditionKey1 = executionConditionKey(testClass, testsInvoked.get(0)); @@ -244,32 +238,21 @@ void instancePerMethodWithNestedTestClass() { String containerExecutionConditionKey = executionConditionKey(testClass, null); String nestedContainerExecutionConditionKey = executionConditionKey(nestedTestClass, null); String nestedTestTemplateKey = testTemplateKey(nestedTestClass, "singletonTest"); + String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass); + String nestedPostProcessTestInstanceKey = postProcessTestInstanceKey(nestedTestClass); String beforeAllCallbackKey = beforeAllCallbackKey(testClass); String afterAllCallbackKey = afterAllCallbackKey(testClass); String outerTestExecutionConditionKey = executionConditionKey(testClass, "outerTest"); - String outerTestPostProcessTestInstanceKey = postProcessTestInstanceKey(testClass, testClass, "outerTest"); String beforeEachCallbackKey = beforeEachCallbackKey(testClass, "outerTest"); String afterEachCallbackKey = afterEachCallbackKey(testClass, "outerTest"); String nestedBeforeAllCallbackKey = beforeAllCallbackKey(nestedTestClass); String nestedAfterAllCallbackKey = afterAllCallbackKey(nestedTestClass); - String outerPostProcessTestInstanceKey1 = postProcessTestInstanceKey(testClass, nestedTestClass, - testsInvoked.get(0)); - String nestedPostProcessTestInstanceKey1 = postProcessTestInstanceKey(nestedTestClass, nestedTestClass, - testsInvoked.get(0)); String nestedExecutionConditionKey1 = executionConditionKey(nestedTestClass, testsInvoked.get(0)); String nestedBeforeEachCallbackKey1 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(0)); String nestedAfterEachCallbackKey1 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(0)); - String outerPostProcessTestInstanceKey2 = postProcessTestInstanceKey(testClass, nestedTestClass, - testsInvoked.get(1)); - String nestedPostProcessTestInstanceKey2 = postProcessTestInstanceKey(nestedTestClass, nestedTestClass, - testsInvoked.get(1)); String nestedExecutionConditionKey2 = executionConditionKey(nestedTestClass, testsInvoked.get(1)); String nestedBeforeEachCallbackKey2 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(1)); String nestedAfterEachCallbackKey2 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(1)); - String outerPostProcessTestInstanceKey3 = postProcessTestInstanceKey(testClass, nestedTestClass, - testsInvoked.get(2)); - String nestedPostProcessTestInstanceKey3 = postProcessTestInstanceKey(nestedTestClass, nestedTestClass, - testsInvoked.get(2)); String nestedExecutionConditionKey3 = executionConditionKey(nestedTestClass, testsInvoked.get(2)); String nestedBeforeEachCallbackKey3 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(2)); String nestedAfterEachCallbackKey3 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(2)); @@ -279,27 +262,22 @@ void instancePerMethodWithNestedTestClass() { containerExecutionConditionKey, nestedTestTemplateKey, nestedContainerExecutionConditionKey, + postProcessTestInstanceKey, + nestedPostProcessTestInstanceKey, beforeAllCallbackKey, afterAllCallbackKey, outerTestExecutionConditionKey, - outerTestPostProcessTestInstanceKey, beforeEachCallbackKey, afterEachCallbackKey, nestedBeforeAllCallbackKey, nestedAfterAllCallbackKey, nestedExecutionConditionKey1, - outerPostProcessTestInstanceKey1, - nestedPostProcessTestInstanceKey1, nestedBeforeEachCallbackKey1, nestedAfterEachCallbackKey1, nestedExecutionConditionKey2, - outerPostProcessTestInstanceKey2, - nestedPostProcessTestInstanceKey2, nestedBeforeEachCallbackKey2, nestedAfterEachCallbackKey2, nestedExecutionConditionKey3, - outerPostProcessTestInstanceKey3, - nestedPostProcessTestInstanceKey3, nestedBeforeEachCallbackKey3, nestedAfterEachCallbackKey3 ); @@ -316,14 +294,12 @@ void instancePerMethodWithNestedTestClass() { assertNotNull(instance); assertSame(instance, instanceMap.get(afterEachCallbackKey)); assertSame(instance, instanceMap.get(outerTestExecutionConditionKey)); - assertSame(instance, instanceMap.get(outerTestPostProcessTestInstanceKey)); Object nestedInstance1 = instanceMap.get(nestedBeforeEachCallbackKey1); assertNotNull(nestedInstance1); assertNotSame(instance, nestedInstance1); assertSame(nestedInstance1, instanceMap.get(nestedAfterEachCallbackKey1)); assertSame(nestedInstance1, instanceMap.get(nestedExecutionConditionKey1)); - assertSame(nestedInstance1, instanceMap.get(nestedPostProcessTestInstanceKey1)); Object nestedInstance2 = instanceMap.get(nestedBeforeEachCallbackKey2); assertNotNull(nestedInstance2); @@ -331,22 +307,24 @@ void instancePerMethodWithNestedTestClass() { assertNotSame(nestedInstance1, nestedInstance2); assertSame(nestedInstance2, instanceMap.get(nestedAfterEachCallbackKey2)); assertSame(nestedInstance2, instanceMap.get(nestedExecutionConditionKey2)); - assertSame(nestedInstance2, instanceMap.get(nestedPostProcessTestInstanceKey2)); - Object nestedInstance3 = instanceMap.get(nestedBeforeEachCallbackKey3); + Object nestedInstance3 = instanceMap.get(nestedPostProcessTestInstanceKey); assertNotNull(nestedInstance3); assertNotSame(instance, nestedInstance3); assertNotSame(nestedInstance1, nestedInstance3); assertSame(nestedInstance3, instanceMap.get(nestedAfterEachCallbackKey3)); assertSame(nestedInstance3, instanceMap.get(nestedExecutionConditionKey3)); - assertSame(nestedInstance3, instanceMap.get(nestedPostProcessTestInstanceKey3)); Object outerInstance1 = ReflectionUtils.getOutermostInstance(nestedInstance1, testClass).get(); Object outerInstance2 = ReflectionUtils.getOutermostInstance(nestedInstance2, testClass).get(); Object outerInstance3 = ReflectionUtils.getOutermostInstance(nestedInstance3, testClass).get(); assertNotSame(outerInstance1, outerInstance2); assertNotSame(outerInstance1, outerInstance3); - assertNotSame(outerInstance2, outerInstance3); + + // The last tracked instance stored under postProcessTestInstanceKey + // is only created in order to instantiate the nested test class for + // test2(). + assertSame(outerInstance3, instanceMap.get(postProcessTestInstanceKey)); } @Test @@ -365,8 +343,8 @@ void instancePerClassWithNestedTestClass() { String containerExecutionConditionKey = executionConditionKey(testClass, null); String nestedContainerExecutionConditionKey = executionConditionKey(nestedTestClass, null); String nestedTestTemplateKey = testTemplateKey(nestedTestClass, "singletonTest"); - String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass, testClass, null); - String nestedPostProcessTestInstanceKey = postProcessTestInstanceKey(nestedTestClass, nestedTestClass, null); + String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass); + String nestedPostProcessTestInstanceKey = postProcessTestInstanceKey(nestedTestClass); String beforeAllCallbackKey = beforeAllCallbackKey(testClass); String afterAllCallbackKey = afterAllCallbackKey(testClass); String outerTestExecutionConditionKey = executionConditionKey(testClass, "outerTest"); @@ -455,9 +433,8 @@ void instancePerMethodOnOuterTestClassWithInstancePerClassOnNestedTestClass() { String containerExecutionConditionKey = executionConditionKey(testClass, null); String nestedContainerExecutionConditionKey = executionConditionKey(nestedTestClass, null); String nestedTestTemplateKey = testTemplateKey(nestedTestClass, "singletonTest"); - String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass, testClass, "outerTest"); - String outerPostProcessTestInstanceKey = postProcessTestInstanceKey(testClass, nestedTestClass, null); - String nestedPostProcessTestInstanceKey = postProcessTestInstanceKey(nestedTestClass, nestedTestClass, null); + String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass); + String nestedPostProcessTestInstanceKey = postProcessTestInstanceKey(nestedTestClass); String beforeAllCallbackKey = beforeAllCallbackKey(testClass); String afterAllCallbackKey = afterAllCallbackKey(testClass); String outerTestExecutionConditionKey = executionConditionKey(testClass, "outerTest"); @@ -481,7 +458,6 @@ void instancePerMethodOnOuterTestClassWithInstancePerClassOnNestedTestClass() { nestedTestTemplateKey, nestedContainerExecutionConditionKey, postProcessTestInstanceKey, - outerPostProcessTestInstanceKey, nestedPostProcessTestInstanceKey, beforeAllCallbackKey, afterAllCallbackKey, @@ -508,7 +484,7 @@ void instancePerMethodOnOuterTestClassWithInstancePerClassOnNestedTestClass() { Object instance = instanceMap.get(beforeEachCallbackKey); assertSame(instance, instanceMap.get(afterEachCallbackKey)); - assertSame(instance, instanceMap.get(postProcessTestInstanceKey)); + assertSame(instance, instanceMap.get(outerTestExecutionConditionKey)); Object nestedInstance = instanceMap.get(nestedPostProcessTestInstanceKey); assertNotNull(nestedInstance); @@ -526,10 +502,12 @@ void instancePerMethodOnOuterTestClassWithInstancePerClassOnNestedTestClass() { assertSame(nestedInstance, instanceMap.get(nestedBeforeEachCallbackKey3)); assertSame(nestedInstance, instanceMap.get(nestedAfterEachCallbackKey3)); + // The last tracked instance stored under postProcessTestInstanceKey + // is only created in order to instantiate the nested test class. Object outerInstance = ReflectionUtils.getOutermostInstance(nestedInstance, testClass).get(); assertEquals(instance.getClass(), outerInstance.getClass()); assertNotSame(instance, outerInstance); - assertSame(outerInstance, instanceMap.get(outerPostProcessTestInstanceKey)); + assertSame(outerInstance, instanceMap.get(postProcessTestInstanceKey)); } private void performAssertions(Class testClass, int containers, int tests, int instances, int nestedInstances, @@ -557,9 +535,8 @@ private static String executionConditionKey(Class testClass, String testMetho return concat(ExecutionCondition.class, testClass, testMethod); } - private static String postProcessTestInstanceKey(Class instantiatedClass, Class testClass, - String testMethod) { - return concat(TestInstancePostProcessor.class, instantiatedClass, testClass, testMethod); + private static String postProcessTestInstanceKey(Class testClass) { + return concat(TestInstancePostProcessor.class, testClass); } private static String beforeAllCallbackKey(Class testClass) { @@ -582,10 +559,6 @@ private static String testTemplateKey(Class testClass, String testMethod) { return concat(TestTemplateInvocationContextProvider.class, testClass, testMethod); } - private static String concat(Class c1, Class c2, Class c3, String str) { - return concat(c1.getSimpleName(), c2.getSimpleName(), c3.getSimpleName(), str); - } - private static String concat(Class c1, Class c2, String str) { return concat(c1.getSimpleName(), c2.getSimpleName(), str); } @@ -686,7 +659,7 @@ static void beforeAll(TestInfo testInfo) { @Test void outerTest() { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), "outerTest"))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); } @AfterAll @@ -712,19 +685,19 @@ void beforeEach() { @Test void test1(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), "test1"))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @Test void test2(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), "test2"))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @SingletonTest void singletonTest(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), "singletonTest"))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @@ -752,7 +725,7 @@ static void beforeAll(TestInfo testInfo) { @Test void outerTest() { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), null))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); } @AfterAll @@ -783,19 +756,19 @@ void beforeEach() { @Test void test1(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), null))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @Test void test2(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), null))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @SingletonTest void singletonTest(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), null))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @@ -829,7 +802,7 @@ void beforeEach() { @Test void outerTest() { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), "outerTest"))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); } @AfterEach @@ -859,19 +832,19 @@ void beforeEach() { @Test void test1(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), null))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @Test void test2(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), null))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @SingletonTest void singletonTest(TestInfo testInfo) { - assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass(), getClass(), null))); + assertSame(this, instanceMap.get(postProcessTestInstanceKey(getClass()))); testsInvoked.add(testInfo.getTestMethod().get().getName()); } @@ -904,15 +877,10 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con } @Override - public void postProcessTestInstance(ExtensionContext context) { - Object testInstance = context.getTestInstance().orElseThrow(() -> { - IllegalStateException exception = new IllegalStateException("test instance must not be null"); - exception.printStackTrace(System.err); - return exception; - }); - String key = postProcessTestInstanceKey(testInstance.getClass(), context.getTestClass().get(), - context.getTestMethod().map(Method::getName).orElse(null)); - instanceMap.put(key, testInstance); + public void postProcessTestInstance(Object testInstance, ExtensionContext context) { + assertNotNull(testInstance); + context.getTestInstance().ifPresent(instance -> assertSame(testInstance, instance)); + instanceMap.put(postProcessTestInstanceKey(context.getTestClass().get()), testInstance); } @Override @@ -927,25 +895,15 @@ public void afterAll(ExtensionContext context) { @Override public void beforeEach(ExtensionContext context) { - Object testInstance = context.getTestInstance().orElseThrow(() -> { - IllegalStateException exception = new IllegalStateException("test instance must not be null"); - exception.printStackTrace(System.err); - return exception; - }); instanceMap.put( beforeEachCallbackKey(context.getTestClass().get(), context.getTestMethod().get().getName()), - testInstance); + context.getTestInstance().orElse(null)); } @Override public void afterEach(ExtensionContext context) { - Object testInstance = context.getTestInstance().orElseThrow(() -> { - IllegalStateException exception = new IllegalStateException("test instance must not be null"); - exception.printStackTrace(System.err); - return exception; - }); instanceMap.put(afterEachCallbackKey(context.getTestClass().get(), context.getTestMethod().get().getName()), - testInstance); + context.getTestInstance().orElse(null)); } @Override @@ -955,9 +913,9 @@ public boolean supportsTestTemplate(ExtensionContext context) { @Override public Stream provideTestTemplateInvocationContexts(ExtensionContext context) { - Object testInstance = context.getTestInstance().orElse(null); + instanceMap.put(testTemplateKey(context.getTestClass().get(), context.getTestMethod().get().getName()), - testInstance); + context.getTestInstance().orElse(null)); return Stream.of(new TestTemplateInvocationContext() { }); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestTemplateInvocationTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestTemplateInvocationTests.java index 1fd49fd136cd..e8aae79d554e 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestTemplateInvocationTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestTemplateInvocationTests.java @@ -508,11 +508,9 @@ public Stream provideTestTemplateInvocationContex private static class SingleInvocationContextProviderWithDisabledInvocations extends SingleInvocationContextProvider { - @Override public Stream provideTestTemplateInvocationContexts(ExtensionContext context) { return Stream.of(new TestTemplateInvocationContext() { - @Override public List getAdditionalExtensions() { return singletonList(new AlwaysDisabledExecutionCondition()); @@ -647,13 +645,7 @@ public String getDisplayName(int invocationIndex) { @Override public List getAdditionalExtensions() { - return singletonList((TestInstancePostProcessor) (context) -> { - Object testInstance = context.getTestInstance().orElseThrow(() -> { - IllegalStateException exception = new IllegalStateException( - "test instance must not be null"); - exception.printStackTrace(System.err); - return exception; - }); + return singletonList((TestInstancePostProcessor) (testInstance, context) -> { Field field = testInstance.getClass().getDeclaredField("parameterInstanceVariable"); field.setAccessible(true); field.set(testInstance, argument); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java index 6d343c676418..f10848feb5de 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java @@ -22,7 +22,7 @@ import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; -import org.junit.jupiter.engine.execution.AbstractExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; import org.junit.jupiter.engine.execution.ThrowableCollector; import org.junit.platform.engine.UniqueId; @@ -36,13 +36,13 @@ class TestFactoryTestDescriptorTests { private JupiterEngineExecutionContext context; - private AbstractExtensionContext extensionContext; + private ExtensionContext extensionContext; private TestFactoryTestDescriptor descriptor; private boolean isClosed; @BeforeEach void before() throws Exception { - extensionContext = mock(AbstractExtensionContext.class); + extensionContext = mock(ExtensionContext.class); isClosed = false; context = new JupiterEngineExecutionContext(null, null).extend().withThrowableCollector( diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ExtensionContextTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ExtensionContextTests.java index bf4b0caf4ca2..eb457890cb8f 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ExtensionContextTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ExtensionContextTests.java @@ -95,7 +95,7 @@ void tagsCanBeRetrievedInExtensionContext() { assertThat(nestedExtensionContext.getTags()).containsExactlyInAnyOrder("outer-tag", "nested-tag"); MethodExtensionContext methodExtensionContext = new MethodExtensionContext(outerExtensionContext, null, - methodTestDescriptor, new ThrowableCollector()); + methodTestDescriptor, new OuterClass(), new ThrowableCollector()); assertThat(methodExtensionContext.getTags()).containsExactlyInAnyOrder("outer-tag", "method-tag"); } @@ -106,8 +106,7 @@ void fromMethodTestDescriptor() { ClassExtensionContext classExtensionContext = new ClassExtensionContext(null, null, classTestDescriptor, null); MethodExtensionContext methodExtensionContext = new MethodExtensionContext(classExtensionContext, null, - methodTestDescriptor, new ThrowableCollector()); - methodExtensionContext.setTestInstance(new OuterClass()); + methodTestDescriptor, new OuterClass(), new ThrowableCollector()); assertAll("methodContext", // () -> assertThat(methodExtensionContext.getTestClass()).contains(OuterClass.class), // () -> assertThat(methodExtensionContext.getDisplayName()).isEqualTo(methodTestDescriptor.getDisplayName()), // @@ -144,9 +143,9 @@ void reportEntriesArePublishedToExecutionContext() { void usingStore() { MethodTestDescriptor methodTestDescriptor = methodDescriptor(); ClassTestDescriptor classTestDescriptor = outerClassDescriptor(methodTestDescriptor); - AbstractExtensionContext parentContext = new ClassExtensionContext(null, null, classTestDescriptor, null); + ExtensionContext parentContext = new ClassExtensionContext(null, null, classTestDescriptor, null); MethodExtensionContext childContext = new MethodExtensionContext(parentContext, null, methodTestDescriptor, - new ThrowableCollector()); + new OuterClass(), new ThrowableCollector()); ExtensionContext.Store childStore = childContext.getStore(); ExtensionContext.Store parentStore = parentContext.getStore(); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java index e34e64942eca..00a303a770bd 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java @@ -159,12 +159,7 @@ void test() { private static class FooInstancePostProcessor implements TestInstancePostProcessor { @Override - public void postProcessTestInstance(ExtensionContext context) throws Exception { - Object testInstance = context.getTestInstance().orElseThrow(() -> { - IllegalStateException exception = new IllegalStateException("test instance must not be null"); - exception.printStackTrace(System.err); - return exception; - }); + public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception { if (testInstance instanceof Named) { ((Named) testInstance).setName("foo"); } @@ -175,12 +170,7 @@ public void postProcessTestInstance(ExtensionContext context) throws Exception { private static class BarInstancePostProcessor implements TestInstancePostProcessor { @Override - public void postProcessTestInstance(ExtensionContext context) throws Exception { - Object testInstance = context.getTestInstance().orElseThrow(() -> { - IllegalStateException exception = new IllegalStateException("test instance must not be null"); - exception.printStackTrace(System.err); - return exception; - }); + public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception { if (testInstance instanceof Named) { ((Named) testInstance).setName("bar"); } diff --git a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java index a11640b12054..180f90b8c61a 100644 --- a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java +++ b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java @@ -107,20 +107,20 @@ void executesLifecycleMethods() { assertThat(LifecycleTestCase.lifecycleEvents).containsExactly( "beforeAll:ParameterizedTestIntegrationTests$LifecycleTestCase", // "providerMethod", - "constructor:[1] foo", + "constructor:ParameterizedTestIntegrationTests$LifecycleTestCase", "beforeEach:[1] foo", testMethods.get(0) + ":[1] foo", "afterEach:[1] foo", - "constructor:[2] bar", + "constructor:ParameterizedTestIntegrationTests$LifecycleTestCase", "beforeEach:[2] bar", testMethods.get(0) + ":[2] bar", "afterEach:[2] bar", "providerMethod", - "constructor:[1] foo", + "constructor:ParameterizedTestIntegrationTests$LifecycleTestCase", "beforeEach:[1] foo", testMethods.get(1) + ":[1] foo", "afterEach:[1] foo", - "constructor:[2] bar", + "constructor:ParameterizedTestIntegrationTests$LifecycleTestCase", "beforeEach:[2] bar", testMethods.get(1) + ":[2] bar", "afterEach:[2] bar",