From ee8cb2e44a92c511bc73553f2005ecc890bec141 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 25 Jun 2017 15:31:12 +0200 Subject: [PATCH] Move getTestInstance() to ExtensionContext Issue: #419 --- .../junit/jupiter/api/extension/ExtensionContext.java | 9 +++++++++ .../jupiter/api/extension/TestExtensionContext.java | 7 ------- .../descriptor/ClassBasedContainerExtensionContext.java | 6 ++++++ .../jupiter/engine/descriptor/ClassTestDescriptor.java | 2 +- .../engine/descriptor/JupiterEngineExtensionContext.java | 6 ++++++ .../descriptor/MethodBasedTestExtensionContext.java | 4 ++-- .../jupiter/engine/descriptor/MethodTestDescriptor.java | 2 +- .../engine/descriptor/TestFactoryTestDescriptor.java | 2 +- .../TestTemplateContainerExtensionContext.java | 6 ++++++ .../junit/jupiter/engine/TestInstanceLifecycleTests.java | 5 +++++ .../descriptor/TestFactoryTestDescriptorTests.java | 2 +- .../jupiter/engine/execution/ExtensionContextTests.java | 5 +++-- .../migrationsupport/rules/AbstractTestRuleSupport.java | 5 +++-- .../jupiter/params/ParameterizedTestExtensionTests.java | 5 +++++ 14 files changed, 49 insertions(+), 17 deletions(-) diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExtensionContext.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExtensionContext.java index c25ec4019123..848d38e9a1e3 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExtensionContext.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExtensionContext.java @@ -111,6 +111,15 @@ public interface ExtensionContext { */ Optional> getTestClass(); + /** + * Get the test instance associated with the current test or container, + * if available. + * + * @return an {@code Optional} containing the test instance; never + * {@code null} but potentially empty + */ + Optional getTestInstance(); + /** * Get the {@link Method} associated with the current test, if available. * diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestExtensionContext.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestExtensionContext.java index 85bccf864e5d..99c9242a2d7f 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestExtensionContext.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestExtensionContext.java @@ -25,13 +25,6 @@ @API(Experimental) public interface TestExtensionContext extends ExtensionContext { - /** - * Get the test instance associated with this {@code TestExtensionContext}. - * - * @return the test instance; never {@code null} - */ - Object getTestInstance(); - /** * Get the exception that was thrown during execution of the test associated * with this {@code TestExtensionContext}, if available. diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedContainerExtensionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedContainerExtensionContext.java index 0982cd65af1a..29345ecd3a4b 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedContainerExtensionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedContainerExtensionContext.java @@ -43,6 +43,12 @@ public Optional> getTestClass() { return Optional.of(getTestDescriptor().getTestClass()); } + @Override + public Optional getTestInstance() { + // TODO Determine how to access the test instance + return null; + } + @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 9146859134a6..3e8a3b97df6c 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 @@ -282,7 +282,7 @@ private AfterEachMethodAdapter synthesizeAfterEachMethodAdapter(Method method) { private void invokeMethodInTestExtensionContext(Method method, TestExtensionContext context, ExtensionRegistry registry) { - Object instance = ReflectionUtils.getOuterInstance(context.getTestInstance(), + Object instance = ReflectionUtils.getOuterInstance(context.getTestInstance().get(), method.getDeclaringClass()).orElseThrow( () -> new JUnitException("Failed to find instance for method: " + method.toGenericString())); 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 31b54478931f..3ca4cc99f501 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 @@ -42,8 +42,14 @@ 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/MethodBasedTestExtensionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodBasedTestExtensionContext.java index 40e06fe95086..d250fe8fc091 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodBasedTestExtensionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodBasedTestExtensionContext.java @@ -56,8 +56,8 @@ public Optional getTestMethod() { } @Override - public Object getTestInstance() { - return this.testInstance; + public Optional getTestInstance() { + return Optional.of(this.testInstance); } @Override 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 5c5aeb099a85..c9d6b4742891 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 @@ -168,7 +168,7 @@ protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTe throwableCollector.execute(() -> { try { Method testMethod = getTestMethod(); - Object instance = testExtensionContext.getTestInstance(); + Object instance = testExtensionContext.getTestInstance().get(); executableInvoker.invoke(testMethod, instance, testExtensionContext, context.getExtensionRegistry()); } catch (Throwable throwable) { diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptor.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptor.java index 7fee87c58038..ee82d59d831c 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptor.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptor.java @@ -66,7 +66,7 @@ protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTe TestExtensionContext testExtensionContext = (TestExtensionContext) context.getExtensionContext(); context.getThrowableCollector().execute(() -> { - Object instance = testExtensionContext.getTestInstance(); + Object instance = testExtensionContext.getTestInstance().get(); Object testFactoryMethodResult = executableInvoker.invoke(getTestMethod(), instance, testExtensionContext, context.getExtensionRegistry()); TestSource source = getSource().orElseThrow(() -> new JUnitException("Test source must be present")); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateContainerExtensionContext.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateContainerExtensionContext.java index 97e2b59b9276..f12cef9cd111 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateContainerExtensionContext.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateContainerExtensionContext.java @@ -43,6 +43,12 @@ public Optional> getTestClass() { return Optional.of(getTestDescriptor().getTestClass()); } + @Override + public Optional getTestInstance() { + // TODO Determine how to access the test instance + return null; + } + @Override public Optional getTestMethod() { return Optional.of(getTestDescriptor().getTestMethod()); 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 f0e736406d7b..90f41ade8585 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 @@ -24,12 +24,17 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.platform.engine.test.event.ExecutionEventRecorder; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; /** * Tests for {@link TestInstance @TestInstance} lifecycle support. * * @since 5.0 */ +@RunWith(JUnitPlatform.class) +public + class TestInstanceLifecycleTests extends AbstractJupiterTestEngineTests { private static int instanceCount; 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 504d80e930a0..87d0e4074033 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 @@ -76,7 +76,7 @@ void streamsFromTestFactoriesShouldBeClosedWhenTheyThrow() { private void prepareMockForTestInstanceWithCustomStream(Stream stream) { Stream mockStream = stream.onClose(() -> isClosed = true); - when(testExtensionContext.getTestInstance()).thenReturn(new CustomStreamTestCase(mockStream)); + when(testExtensionContext.getTestInstance()).thenReturn(Optional.of(new CustomStreamTestCase(mockStream))); } private static class CustomStreamTestCase { 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 308fd0c7f17b..85dc529f708a 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 @@ -114,7 +114,7 @@ void fromMethodTestDescriptor() { () -> assertThat(testExtensionContext.getTestClass()).contains(OuterClass.class), // () -> assertThat(testExtensionContext.getDisplayName()).isEqualTo(methodTestDescriptor.getDisplayName()), // () -> assertThat(testExtensionContext.getParent()).contains(classExtensionContext), // - () -> assertThat(testExtensionContext.getTestInstance()).isExactlyInstanceOf(OuterClass.class) // + () -> assertThat(testExtensionContext.getTestInstance().get()).isExactlyInstanceOf(OuterClass.class) // ); } @@ -187,8 +187,9 @@ private ClassTestDescriptor nestedClassDescriptor() { private ClassTestDescriptor outerClassDescriptor(TestDescriptor child) { ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(UniqueId.root("class", "OuterClass"), OuterClass.class); - if (child != null) + if (child != null) { classTestDescriptor.addChild(child); + } return classTestDescriptor; } diff --git a/junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/AbstractTestRuleSupport.java b/junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/AbstractTestRuleSupport.java index c0a8b63fd759..46d43a23796b 100644 --- a/junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/AbstractTestRuleSupport.java +++ b/junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/AbstractTestRuleSupport.java @@ -72,11 +72,12 @@ public void afterEach(TestExtensionContext context) throws Exception { private void invokeAppropriateMethodOnRuleAnnotatedMembers(TestExtensionContext context, Consumer methodCaller) { - List members = findRuleAnnotatedMembers(context.getTestInstance()); + Object testInstance = context.getTestInstance().get(); + List members = findRuleAnnotatedMembers(testInstance); // @formatter:off members.stream() - .map(member -> TestRuleAnnotatedMemberFactory.from(context.getTestInstance(), member)) + .map(member -> TestRuleAnnotatedMemberFactory.from(testInstance, member)) .map(this.adapterGenerator) .forEach(methodCaller::accept); // @formatter:on diff --git a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestExtensionTests.java b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestExtensionTests.java index ef24108c34f7..8b27641bfda9 100644 --- a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestExtensionTests.java +++ b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestExtensionTests.java @@ -129,6 +129,11 @@ public Optional> getTestClass() { return null; } + @Override + public java.util.Optional getTestInstance() { + return Optional.empty(); + } + @Override public void publishReportEntry(Map map) { }