Skip to content

Commit 9af6ca4

Browse files
committed
Stop logging exceptions thrown from ExtensionContext.close()
Prior to this commit such exceptions were logged and rethrown which caused them to be reported twice: once in the log and once as part of the test failure. Fixes #3422.
1 parent e4a3980 commit 9af6ca4

4 files changed

Lines changed: 20 additions & 21 deletions

File tree

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public void close() throws Exception {
5353
((AutoCloseable) extensionContext).close();
5454
}
5555
catch (Exception e) {
56-
logger.error(e, () -> "Caught exception while closing extension context: " + extensionContext);
57-
throw e;
56+
throw new JUnitException("Failed to close extension context", e);
5857
}
5958
}
6059
}

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/CloseableResourceIntegrationTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
1616
import static org.junit.platform.testkit.engine.EventConditions.reportEntry;
1717
import static org.junit.platform.testkit.engine.EventConditions.test;
18+
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.cause;
1819
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message;
1920
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.suppressed;
2021

@@ -57,7 +58,10 @@ void exceptionsDuringCloseAreReportedAsSuppressed() {
5758
test(), //
5859
finishedWithFailure( //
5960
message("Exception in test"), //
60-
suppressed(0, message("Exception in onClose")))));
61+
suppressed(0, //
62+
message("Failed to close extension context"), //
63+
cause(message("Exception in onClose")) //
64+
))));
6165
}
6266

6367
@ExtendWith(ThrowingOnCloseExtension.class)

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContextTests.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@
1717
import static org.mockito.Mockito.mock;
1818
import static org.mockito.Mockito.withSettings;
1919

20-
import java.util.logging.Level;
21-
import java.util.logging.LogRecord;
22-
2320
import org.junit.jupiter.api.Test;
2421
import org.junit.jupiter.api.extension.ExtensionContext;
25-
import org.junit.jupiter.api.fixtures.TrackLogRecords;
2622
import org.junit.jupiter.engine.config.JupiterConfiguration;
2723
import org.junit.jupiter.engine.extension.MutableExtensionRegistry;
28-
import org.junit.platform.commons.logging.LogRecordListener;
2924
import org.junit.platform.engine.EngineExecutionListener;
3025

3126
/**
@@ -89,23 +84,21 @@ void canOverrideAttributeWhenContextIsExtended() {
8984
}
9085

9186
@Test
92-
@TrackLogRecords
93-
void closeAttemptExceptionWillBeThrownDownTheCallStack(LogRecordListener logRecordListener) throws Exception {
87+
void closeAttemptExceptionWillBeThrownDownTheCallStack() throws Exception {
9488
ExtensionContext failingExtensionContext = mock(ExtensionContext.class,
9589
withSettings().extraInterfaces(AutoCloseable.class));
96-
Exception expectedException = new Exception("test message");
97-
doThrow(expectedException).when(((AutoCloseable) failingExtensionContext)).close();
90+
Exception expectedCause = new Exception("test message");
91+
doThrow(expectedCause).when(((AutoCloseable) failingExtensionContext)).close();
9892

9993
JupiterEngineExecutionContext newContext = originalContext.extend() //
10094
.withExtensionContext(failingExtensionContext) //
10195
.build();
10296

10397
Exception actualException = assertThrows(Exception.class, newContext::close);
10498

105-
assertSame(expectedException, actualException);
106-
assertThat(logRecordListener.stream(JupiterEngineExecutionContext.class, Level.SEVERE)) //
107-
.extracting(LogRecord::getMessage) //
108-
.containsOnly("Caught exception while closing extension context: " + failingExtensionContext);
99+
assertThat(actualException) //
100+
.hasMessage("Failed to close extension context") //
101+
.hasCauseReference(expectedCause);
109102
}
110103

111104
}

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryPerDeclarationTests.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,14 @@ private void onlyAttemptsToDeleteUndeletablePathOnce(Class<? extends Undeletable
230230
it -> Path.of(it.getKeyValuePairs().get(UndeletableTestCase.TEMP_DIR))).findAny().orElseThrow();
231231

232232
assertSingleFailedTest(results, //
233-
instanceOf(IOException.class), //
234-
message("Failed to delete temp directory " + tempDir.toAbsolutePath() + ". " + //
235-
"The following paths could not be deleted (see suppressed exceptions for details): <root>, undeletable"), //
236-
suppressed(0, instanceOf(DirectoryNotEmptyException.class)), //
237-
suppressed(1, instanceOf(IOException.class), message("Simulated failure")));
233+
cause( //
234+
instanceOf(IOException.class), //
235+
message("Failed to delete temp directory " + tempDir.toAbsolutePath() + ". " + //
236+
"The following paths could not be deleted (see suppressed exceptions for details): <root>, undeletable"), //
237+
suppressed(0, instanceOf(DirectoryNotEmptyException.class)), //
238+
suppressed(1, instanceOf(IOException.class), message("Simulated failure")) //
239+
) //
240+
);
238241
}
239242

240243
@Nested

0 commit comments

Comments
 (0)