Skip to content

Commit 46e83ca

Browse files
committed
Polishing
Issue: #910
1 parent 7abfa00 commit 46e83ca

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ private TestInstanceProvider testInstanceProvider(JupiterEngineExecutionContext
176176
if (this.lifecycle == Lifecycle.PER_CLASS) {
177177
// Eagerly load test instance for BeforeAllCallbacks, if necessary,
178178
// and store the instance in the ExtensionContext.
179+
// Note: as a side effect, instantiateAndPostProcessTestInstance() also stores
180+
// the instance it creates in the "current" extension context.
179181
Object instance = instantiateAndPostProcessTestInstance(parentExecutionContext, extensionContext, registry);
182+
183+
// Return a TestInstanceProvider that additionally sets the test instance
184+
// in the supplied child extension context (e.g., a MethodExtensionContext).
180185
return (childContext, childRegistry) -> {
181186
childContext.setTestInstance(instance);
182187
return instance;
@@ -188,6 +193,14 @@ private TestInstanceProvider testInstanceProvider(JupiterEngineExecutionContext
188193
childContext, childRegistry.orElse(registry));
189194
}
190195

196+
/**
197+
* Instantiate the test instance; set the test instance in the supplied
198+
* extension context; and post process the test instance.
199+
*
200+
* @see #instantiateTestClass
201+
* @see AbstractExtensionContext#setTestInstance
202+
* @see #invokeTestInstancePostProcessors
203+
*/
191204
private Object instantiateAndPostProcessTestInstance(JupiterEngineExecutionContext context,
192205
AbstractExtensionContext<?> extensionContext, ExtensionRegistry registry) {
193206

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

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext conte
8282
ThrowableCollector throwableCollector = new ThrowableCollector();
8383
AbstractExtensionContext<?> extensionContext = new MethodExtensionContext(context.getExtensionContext(),
8484
context.getExecutionListener(), this, throwableCollector);
85+
86+
// Even though we (intentionally) ignore the return value, the following line
87+
// is required since the configured TestInstanceProvider is responsible for
88+
// setting the test instance in the supplied extension context.
89+
// See ClassTestDescriptor#testInstanceProvider(...) for details.
8590
context.getTestInstanceProvider().getTestInstance(extensionContext, Optional.of(registry));
8691

8792
// @formatter:off
@@ -100,6 +105,7 @@ protected ExtensionRegistry populateNewExtensionRegistry(JupiterEngineExecutionC
100105
@Override
101106
public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context,
102107
DynamicTestExecutor dynamicTestExecutor) throws Exception {
108+
103109
ThrowableCollector throwableCollector = context.getThrowableCollector();
104110

105111
// @formatter:off

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext conte
6161

6262
AbstractExtensionContext<?> extensionContext = new TestTemplateExtensionContext(context.getExtensionContext(),
6363
context.getExecutionListener(), this);
64-
// We don't require a test instance here but provide it to extensions should the enclosing class's
65-
// ExtensionContext already contain one.
64+
65+
// We don't require a test instance here, but we are responsible for providing it
66+
// to extensions if the enclosing class's ExtensionContext already contains one.
67+
// See ClassTestDescriptor#testInstanceProvider(...) for details.
6668
extensionContext.setTestInstance(context.getExtensionContext().getTestInstance().orElse(null));
6769

6870
// @formatter:off

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

-1
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,6 @@ public void postProcessTestInstance(ExtensionContext context) {
912912
});
913913
String key = postProcessTestInstanceKey(testInstance.getClass(), context.getTestClass().get(),
914914
context.getTestMethod().map(Method::getName).orElse(null));
915-
System.out.println(testInstance.getClass() + " : " + key);
916915
instanceMap.put(key, testInstance);
917916
}
918917

0 commit comments

Comments
 (0)