diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeImageTest.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeImageTest.java index b53af9748d040..1bf824b77c9fa 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeImageTest.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeImageTest.java @@ -29,4 +29,9 @@ @ExtendWith({ DisabledOnNativeImageCondition.class, QuarkusTestExtension.class, NativeTestExtension.class }) @Retention(RetentionPolicy.RUNTIME) public @interface NativeImageTest { + /** + * Allow to define system properties that will be passed to the native image that will be started by the tests. + * Properties defined in this field will have precedence over the ones defined in profiles shipped with the image. + */ + String[] systemProperties() default {}; } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeTestExtension.java index c6d7775c12302..4d2baf19aebc9 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeTestExtension.java @@ -3,6 +3,7 @@ import java.io.Closeable; import java.io.IOException; import java.lang.reflect.Field; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -71,6 +72,11 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception { Map systemProps = testResourceManager.start(); NativeImageLauncher launcher = new NativeImageLauncher(testClass); launcher.addSystemProperties(systemProps); + systemProps = getAnnotationSystemProperties(testClass); + if (!systemProps.isEmpty()) { + launcher.addSystemProperties(systemProps); + } + try { launcher.start(); } catch (IOException e) { @@ -109,7 +115,31 @@ private void ensureNoInjectAnnotationIsUsed(Class testClass) { } current = current.getSuperclass(); } + } + + private Map getAnnotationSystemProperties(Class testClass) { + final Map systemProps = new HashMap<>(); + final NativeImageTest annotation = testClass.getAnnotation(NativeImageTest.class); + if (annotation != null && annotation.systemProperties().length > 0) { + for (String expression : annotation.systemProperties()) { + if (!expression.contains("=") || expression.startsWith("=")) { + throw new JUnitException("Invalid system property '" + expression + + "' passed to @NativeImageTest: it must be 'key=value'"); + } + final int equalSignIndex = expression.indexOf('='); + final String key = expression.substring(0, equalSignIndex); + + final String value; + if (expression.endsWith("=")) { + value = ""; + } else { + value = expression.substring(equalSignIndex + 1); + } + systemProps.put(key, value); + } + } + return systemProps; } /**