Skip to content

Commit ee8cb2e

Browse files
committedJun 30, 2017
Move getTestInstance() to ExtensionContext
Issue: #419
1 parent 6f4244f commit ee8cb2e

File tree

14 files changed

+49
-17
lines changed

14 files changed

+49
-17
lines changed
 

‎junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExtensionContext.java

+9
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ public interface ExtensionContext {
111111
*/
112112
Optional<Class<?>> getTestClass();
113113

114+
/**
115+
* Get the test instance associated with the current test or container,
116+
* if available.
117+
*
118+
* @return an {@code Optional} containing the test instance; never
119+
* {@code null} but potentially empty
120+
*/
121+
Optional<Object> getTestInstance();
122+
114123
/**
115124
* Get the {@link Method} associated with the current test, if available.
116125
*

‎junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestExtensionContext.java

-7
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
@API(Experimental)
2626
public interface TestExtensionContext extends ExtensionContext {
2727

28-
/**
29-
* Get the test instance associated with this {@code TestExtensionContext}.
30-
*
31-
* @return the test instance; never {@code null}
32-
*/
33-
Object getTestInstance();
34-
3528
/**
3629
* Get the exception that was thrown during execution of the test associated
3730
* with this {@code TestExtensionContext}, if available.

‎junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedContainerExtensionContext.java

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public Optional<Class<?>> getTestClass() {
4343
return Optional.of(getTestDescriptor().getTestClass());
4444
}
4545

46+
@Override
47+
public Optional<Object> getTestInstance() {
48+
// TODO Determine how to access the test instance
49+
return null;
50+
}
51+
4652
@Override
4753
public Optional<Method> getTestMethod() {
4854
return Optional.empty();

‎junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ private AfterEachMethodAdapter synthesizeAfterEachMethodAdapter(Method method) {
282282
private void invokeMethodInTestExtensionContext(Method method, TestExtensionContext context,
283283
ExtensionRegistry registry) {
284284

285-
Object instance = ReflectionUtils.getOuterInstance(context.getTestInstance(),
285+
Object instance = ReflectionUtils.getOuterInstance(context.getTestInstance().get(),
286286
method.getDeclaringClass()).orElseThrow(
287287
() -> new JUnitException("Failed to find instance for method: " + method.toGenericString()));
288288

‎junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterEngineExtensionContext.java

+6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ public Optional<Class<?>> getTestClass() {
4242
return Optional.empty();
4343
}
4444

45+
@Override
46+
public Optional<Object> getTestInstance() {
47+
return Optional.empty();
48+
}
49+
4550
@Override
4651
public Optional<Method> getTestMethod() {
4752
return Optional.empty();
4853
}
54+
4955
}

‎junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodBasedTestExtensionContext.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public Optional<Method> getTestMethod() {
5656
}
5757

5858
@Override
59-
public Object getTestInstance() {
60-
return this.testInstance;
59+
public Optional<Object> getTestInstance() {
60+
return Optional.of(this.testInstance);
6161
}
6262

6363
@Override

‎junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodTestDescriptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTe
168168
throwableCollector.execute(() -> {
169169
try {
170170
Method testMethod = getTestMethod();
171-
Object instance = testExtensionContext.getTestInstance();
171+
Object instance = testExtensionContext.getTestInstance().get();
172172
executableInvoker.invoke(testMethod, instance, testExtensionContext, context.getExtensionRegistry());
173173
}
174174
catch (Throwable throwable) {

‎junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTe
6666
TestExtensionContext testExtensionContext = (TestExtensionContext) context.getExtensionContext();
6767

6868
context.getThrowableCollector().execute(() -> {
69-
Object instance = testExtensionContext.getTestInstance();
69+
Object instance = testExtensionContext.getTestInstance().get();
7070
Object testFactoryMethodResult = executableInvoker.invoke(getTestMethod(), instance, testExtensionContext,
7171
context.getExtensionRegistry());
7272
TestSource source = getSource().orElseThrow(() -> new JUnitException("Test source must be present"));

‎junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateContainerExtensionContext.java

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public Optional<Class<?>> getTestClass() {
4343
return Optional.of(getTestDescriptor().getTestClass());
4444
}
4545

46+
@Override
47+
public Optional<Object> getTestInstance() {
48+
// TODO Determine how to access the test instance
49+
return null;
50+
}
51+
4652
@Override
4753
public Optional<Method> getTestMethod() {
4854
return Optional.of(getTestDescriptor().getTestMethod());

‎junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestInstanceLifecycleTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
import org.junit.jupiter.api.TestInstance;
2525
import org.junit.jupiter.api.TestInstance.Lifecycle;
2626
import org.junit.platform.engine.test.event.ExecutionEventRecorder;
27+
import org.junit.platform.runner.JUnitPlatform;
28+
import org.junit.runner.RunWith;
2729

2830
/**
2931
* Tests for {@link TestInstance @TestInstance} lifecycle support.
3032
*
3133
* @since 5.0
3234
*/
35+
@RunWith(JUnitPlatform.class)
36+
public
37+
3338
class TestInstanceLifecycleTests extends AbstractJupiterTestEngineTests {
3439

3540
private static int instanceCount;

‎junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void streamsFromTestFactoriesShouldBeClosedWhenTheyThrow() {
7676

7777
private void prepareMockForTestInstanceWithCustomStream(Stream<?> stream) {
7878
Stream<?> mockStream = stream.onClose(() -> isClosed = true);
79-
when(testExtensionContext.getTestInstance()).thenReturn(new CustomStreamTestCase(mockStream));
79+
when(testExtensionContext.getTestInstance()).thenReturn(Optional.of(new CustomStreamTestCase(mockStream)));
8080
}
8181

8282
private static class CustomStreamTestCase {

‎junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ExtensionContextTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void fromMethodTestDescriptor() {
114114
() -> assertThat(testExtensionContext.getTestClass()).contains(OuterClass.class), //
115115
() -> assertThat(testExtensionContext.getDisplayName()).isEqualTo(methodTestDescriptor.getDisplayName()), //
116116
() -> assertThat(testExtensionContext.getParent()).contains(classExtensionContext), //
117-
() -> assertThat(testExtensionContext.getTestInstance()).isExactlyInstanceOf(OuterClass.class) //
117+
() -> assertThat(testExtensionContext.getTestInstance().get()).isExactlyInstanceOf(OuterClass.class) //
118118
);
119119
}
120120

@@ -187,8 +187,9 @@ private ClassTestDescriptor nestedClassDescriptor() {
187187
private ClassTestDescriptor outerClassDescriptor(TestDescriptor child) {
188188
ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(UniqueId.root("class", "OuterClass"),
189189
OuterClass.class);
190-
if (child != null)
190+
if (child != null) {
191191
classTestDescriptor.addChild(child);
192+
}
192193
return classTestDescriptor;
193194
}
194195

‎junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/AbstractTestRuleSupport.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ public void afterEach(TestExtensionContext context) throws Exception {
7272
private void invokeAppropriateMethodOnRuleAnnotatedMembers(TestExtensionContext context,
7373
Consumer<GenericBeforeAndAfterAdvice> methodCaller) {
7474

75-
List<T> members = findRuleAnnotatedMembers(context.getTestInstance());
75+
Object testInstance = context.getTestInstance().get();
76+
List<T> members = findRuleAnnotatedMembers(testInstance);
7677

7778
// @formatter:off
7879
members.stream()
79-
.map(member -> TestRuleAnnotatedMemberFactory.from(context.getTestInstance(), member))
80+
.map(member -> TestRuleAnnotatedMemberFactory.from(testInstance, member))
8081
.map(this.adapterGenerator)
8182
.forEach(methodCaller::accept);
8283
// @formatter:on

‎junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestExtensionTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public Optional<Class<?>> getTestClass() {
129129
return null;
130130
}
131131

132+
@Override
133+
public java.util.Optional<Object> getTestInstance() {
134+
return Optional.empty();
135+
}
136+
132137
@Override
133138
public void publishReportEntry(Map<String, String> map) {
134139
}

0 commit comments

Comments
 (0)
Please sign in to comment.