Skip to content

Commit 7b9267e

Browse files
committed
Introduce getRequiredTestInstance() in JupiterTestDescriptor
Issue: #419
1 parent fa7f256 commit 7b9267e

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ private AfterEachMethodAdapter synthesizeAfterEachMethodAdapter(Method method) {
290290

291291
private void invokeMethodInExtensionContext(Method method, ExtensionContext context, ExtensionRegistry registry) {
292292

293-
Object testInstance = context.getTestInstance().orElseThrow(() -> new JUnitException(
294-
"Illegal state: test instance not present for method: " + method.toGenericString()));
295-
293+
Object testInstance = getRequiredTestInstance(context);
296294
testInstance = ReflectionUtils.getOutermostInstance(testInstance, method.getDeclaringClass()).orElseThrow(
297295
() -> new JUnitException("Failed to find instance for method: " + method.toGenericString()));
298296

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
3131
import org.junit.jupiter.api.extension.ExtendWith;
3232
import org.junit.jupiter.api.extension.Extension;
33+
import org.junit.jupiter.api.extension.ExtensionContext;
3334
import org.junit.jupiter.api.function.Executable;
3435
import org.junit.jupiter.engine.execution.ConditionEvaluator;
3536
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
3637
import org.junit.jupiter.engine.extension.ExtensionRegistry;
3738
import org.junit.platform.commons.meta.API;
3839
import org.junit.platform.commons.util.ExceptionUtils;
40+
import org.junit.platform.commons.util.Preconditions;
3941
import org.junit.platform.commons.util.StringUtils;
4042
import org.junit.platform.engine.TestTag;
4143
import org.junit.platform.engine.UniqueId;
@@ -134,4 +136,9 @@ protected void executeAndMaskThrowable(Executable executable) {
134136
}
135137
}
136138

139+
protected Object getRequiredTestInstance(ExtensionContext extensionContext) {
140+
return Preconditions.notNull(extensionContext.getTestInstance().orElse(null),
141+
"Illegal state: required test instance is not present");
142+
}
143+
137144
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
3232
import org.junit.jupiter.engine.execution.ThrowableCollector;
3333
import org.junit.jupiter.engine.extension.ExtensionRegistry;
34-
import org.junit.platform.commons.JUnitException;
3534
import org.junit.platform.commons.meta.API;
3635
import org.junit.platform.commons.util.ExceptionUtils;
3736
import org.junit.platform.engine.TestDescriptor;
@@ -164,8 +163,7 @@ protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTe
164163
throwableCollector.execute(() -> {
165164
try {
166165
Method testMethod = getTestMethod();
167-
Object instance = extensionContext.getTestInstance().orElseThrow(() -> new JUnitException(
168-
"Illegal state: test instance not present for method: " + testMethod.toGenericString()));
166+
Object instance = getRequiredTestInstance(extensionContext);
169167
executableInvoker.invoke(testMethod, instance, extensionContext, context.getExtensionRegistry());
170168
}
171169
catch (Throwable throwable) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTe
6666
ExtensionContext extensionContext = context.getExtensionContext();
6767

6868
context.getThrowableCollector().execute(() -> {
69-
Object instance = extensionContext.getTestInstance().orElseThrow(() -> new JUnitException(
70-
"Illegal state: test instance not present for method: " + getTestMethod().toGenericString()));
69+
Object instance = getRequiredTestInstance(extensionContext);
7170
Object testFactoryMethodResult = executableInvoker.invoke(getTestMethod(), instance, extensionContext,
7271
context.getExtensionRegistry());
7372
TestSource source = getSource().orElseThrow(

0 commit comments

Comments
 (0)