diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/ConfigUtil.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/ConfigUtil.java new file mode 100644 index 0000000000000..c7c0c5ebc1488 --- /dev/null +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/ConfigUtil.java @@ -0,0 +1,30 @@ +package io.quarkus.test; + +import io.quarkus.runtime.configuration.QuarkusConfigFactory; + +public final class ConfigUtil { + private ConfigUtil() { + } + + /** + * Clean up config left over from badly-behaving previous tests. + *

+ * Tests may call ConfigProvider.getConfig() directly or indirectly, + * triggering lazy initialization in QuarkusConfigFactory#getConfigFor, + * and if they don't call QuarkusConfigFactory.setConfig(null) afterward, + * they may leak that config. + * As it's quite hard to guarantee that all tests clean up after them, + * especially if they don't use any Quarkus*Test extensions, + * we call this cleanup here in Quarkus*Test extensions as an additional safeguard. + *

+ * Note this must be called quite early in extensions in order to be effective: + * things like TestHTTPResourceManager#getUri make use of config and may be called very early, + * upon test instantiation, so cleaning up in beforeEach() won't cut it for example. + */ + public static void cleanUp() { + try { + QuarkusConfigFactory.setConfig(null); + } catch (Throwable ignored) { + } + } +} diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusDevModeTest.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusDevModeTest.java index c80f987df6caf..79cc8f6b2c1b7 100644 --- a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusDevModeTest.java +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusDevModeTest.java @@ -228,6 +228,7 @@ public Object createTestInstance(TestInstanceFactoryContext factoryContext, Exte @Override public void beforeAll(ExtensionContext context) throws Exception { + ConfigUtil.cleanUp(); GroovyClassValue.disable(); //set the right launch mode in the outer CL, used by the HTTP host config source ProfileManager.setLaunchMode(LaunchMode.DEVELOPMENT); @@ -324,6 +325,7 @@ public void afterAll(ExtensionContext context) throws Exception { inMemoryLogHandler.clearRecords(); inMemoryLogHandler.setFilter(null); ClearCache.clearAnnotationCache(); + ConfigUtil.cleanUp(); } @Override diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusProdModeTest.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusProdModeTest.java index 3e5b7c72ef43f..936ce48d4cb6e 100644 --- a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusProdModeTest.java +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusProdModeTest.java @@ -365,6 +365,7 @@ private JavaArchive getArchiveProducerOrDefault() { @Override public void beforeAll(ExtensionContext extensionContext) throws Exception { + ConfigUtil.cleanUp(); ensureNoInjectAnnotationIsUsed(extensionContext.getRequiredTestClass()); ExclusivityChecker.checkTestType(extensionContext, QuarkusProdModeTest.class); @@ -757,6 +758,8 @@ public void afterAll(ExtensionContext extensionContext) throws Exception { if ((outputDir != null) && !preventOutputDirCleanup) { FileUtil.deleteDirectory(outputDir); } + + ConfigUtil.cleanUp(); } } diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java index f17fd2e19514c..53c82ed45d735 100644 --- a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java @@ -507,6 +507,7 @@ private void runExtensionMethod(ReflectiveInvocationContext invocationCo @Override public void beforeAll(ExtensionContext extensionContext) throws Exception { + ConfigUtil.cleanUp(); GroovyClassValue.disable(); //set the right launch mode in the outer CL, used by the HTTP host config source ProfileManager.setLaunchMode(LaunchMode.TEST); @@ -766,6 +767,7 @@ public void afterAll(ExtensionContext extensionContext) throws Exception { afterAllCustomizer.run(); } ClearCache.clearAnnotationCache(); + ConfigUtil.cleanUp(); } if (records != null) { assertLogRecords.accept(records);