Skip to content

Commit 210979b

Browse files
committed
Improve diagnostics in failing tests
Issue: #910
1 parent e7b3ab4 commit 210979b

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

Diff for: junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestInstanceLifecycleTests.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,11 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
878878

879879
@Override
880880
public void postProcessTestInstance(ExtensionContext context) {
881-
Object testInstance = context.getTestInstance().orElse(null);
881+
Object testInstance = context.getTestInstance().orElseThrow(() -> {
882+
IllegalStateException exception = new IllegalStateException("test instance must not be null");
883+
exception.printStackTrace(System.err);
884+
return exception;
885+
});
882886
instanceMap.put(postProcessTestInstanceKey(context.getTestClass().get()), testInstance);
883887
}
884888

@@ -894,15 +898,25 @@ public void afterAll(ExtensionContext context) {
894898

895899
@Override
896900
public void beforeEach(ExtensionContext context) {
901+
Object testInstance = context.getTestInstance().orElseThrow(() -> {
902+
IllegalStateException exception = new IllegalStateException("test instance must not be null");
903+
exception.printStackTrace(System.err);
904+
return exception;
905+
});
897906
instanceMap.put(
898907
beforeEachCallbackKey(context.getTestClass().get(), context.getTestMethod().get().getName()),
899-
context.getTestInstance().orElse(null));
908+
testInstance);
900909
}
901910

902911
@Override
903912
public void afterEach(ExtensionContext context) {
913+
Object testInstance = context.getTestInstance().orElseThrow(() -> {
914+
IllegalStateException exception = new IllegalStateException("test instance must not be null");
915+
exception.printStackTrace(System.err);
916+
return exception;
917+
});
904918
instanceMap.put(afterEachCallbackKey(context.getTestClass().get(), context.getTestMethod().get().getName()),
905-
context.getTestInstance().orElse(null));
919+
testInstance);
906920
}
907921

908922
@Override
@@ -912,9 +926,13 @@ public boolean supportsTestTemplate(ExtensionContext context) {
912926

913927
@Override
914928
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
915-
929+
Object testInstance = context.getTestInstance().orElseThrow(() -> {
930+
IllegalStateException exception = new IllegalStateException("test instance must not be null");
931+
exception.printStackTrace(System.err);
932+
return exception;
933+
});
916934
instanceMap.put(testTemplateKey(context.getTestClass().get(), context.getTestMethod().get().getName()),
917-
context.getTestInstance().orElse(null));
935+
testInstance);
918936

919937
return Stream.of(new TestTemplateInvocationContext() {
920938
});

Diff for: junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestTemplateInvocationTests.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,12 @@ public String getDisplayName(int invocationIndex) {
648648
@Override
649649
public List<Extension> getAdditionalExtensions() {
650650
return singletonList((TestInstancePostProcessor) (context) -> {
651-
Object testInstance = context.getTestInstance().orElse(null);
651+
Object testInstance = context.getTestInstance().orElseThrow(() -> {
652+
IllegalStateException exception = new IllegalStateException(
653+
"test instance must not be null");
654+
exception.printStackTrace(System.err);
655+
return exception;
656+
});
652657
Field field = testInstance.getClass().getDeclaredField("parameterInstanceVariable");
653658
field.setAccessible(true);
654659
field.set(testInstance, argument);

Diff for: junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestInstancePostProcessorTests.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ private static class FooInstancePostProcessor implements TestInstancePostProcess
160160

161161
@Override
162162
public void postProcessTestInstance(ExtensionContext context) throws Exception {
163-
Object testInstance = context.getTestInstance().orElse(null);
163+
Object testInstance = context.getTestInstance().orElseThrow(() -> {
164+
IllegalStateException exception = new IllegalStateException("test instance must not be null");
165+
exception.printStackTrace(System.err);
166+
return exception;
167+
});
164168
if (testInstance instanceof Named) {
165169
((Named) testInstance).setName("foo");
166170
}
@@ -172,7 +176,11 @@ private static class BarInstancePostProcessor implements TestInstancePostProcess
172176

173177
@Override
174178
public void postProcessTestInstance(ExtensionContext context) throws Exception {
175-
Object testInstance = context.getTestInstance().orElse(null);
179+
Object testInstance = context.getTestInstance().orElseThrow(() -> {
180+
IllegalStateException exception = new IllegalStateException("test instance must not be null");
181+
exception.printStackTrace(System.err);
182+
return exception;
183+
});
176184
if (testInstance instanceof Named) {
177185
((Named) testInstance).setName("bar");
178186
}

0 commit comments

Comments
 (0)