diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtensionState.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtensionState.java index 6024cb50f400c0..38e5af615cb98b 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtensionState.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtensionState.java @@ -12,23 +12,15 @@ public class QuarkusTestExtensionState implements ExtensionContext.Store.Closeab private final AtomicBoolean closed = new AtomicBoolean(); - protected final Closeable testResourceManager; - protected final Closeable resource; - private final Thread shutdownHook; + protected Closeable testResourceManager; + protected Closeable resource; + private Thread shutdownHook; private Throwable testErrorCause; public QuarkusTestExtensionState(Closeable testResourceManager, Closeable resource) { this.testResourceManager = testResourceManager; this.resource = resource; - this.shutdownHook = new Thread(new Runnable() { - @Override - public void run() { - try { - QuarkusTestExtensionState.this.close(); - } catch (IOException ignored) { - } - } - }, "Quarkus Test Cleanup Shutdown task"); + this.shutdownHook = new ShutdownThread(this); Runtime.getRuntime().addShutdownHook(shutdownHook); } @@ -45,6 +37,10 @@ public void close() throws IOException { Runtime.getRuntime().removeShutdownHook(shutdownHook); } catch (Throwable t) { //won't work if we are already shutting down + } finally { + shutdownHook = null; + testResourceManager = null; + resource = null; } } } @@ -69,4 +65,28 @@ protected void setTestFailed(Throwable failure) { protected void doClose() throws IOException { } + + private static final class ShutdownThread extends Thread { + + public ShutdownThread(QuarkusTestExtensionState state) { + super(new ShutdownHook(state), "Quarkus Test Cleanup Shutdown Thread"); + } + } + + private static final class ShutdownHook implements Runnable { + + private final QuarkusTestExtensionState state; + + private ShutdownHook(QuarkusTestExtensionState state) { + this.state = state; + } + + @Override + public void run() { + try { + state.close(); + } catch (IOException ignored) { + } + } + } }