Skip to content

Commit 56f5850

Browse files
committed
QuarkusComponentTest: skip system config sources by default
This is a breaking change but I believe that it makes sense and the main goal is to mitigate the problem with Quarkus test profiles described in #48899.
1 parent 092de95 commit 56f5850

File tree

6 files changed

+45
-8
lines changed

6 files changed

+45
-8
lines changed

docs/src/main/asciidoc/testing-components.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ NOTE: `@io.quarkus.test.component.TestConfigProperty` declared on a `@Nested` te
297297

298298
CDI beans are also automatically registered for all injected https://smallrye.io/smallrye-config/Main/config/mappings/[Config Mappings]. The mappings are populated with the test configuration properties.
299299

300+
=== Config sources
301+
302+
By default, only the config properties from `application.properties` and properties set by the `@TestConfigProperty` annotation or with the `QuarkusComponentTestExtensionBuilder#configProperty(String, String)` method are included in the test config.
303+
System properties and ENV variables are _not_ included in the test config by default.
304+
However, you can use `@QuarkusComponentTest#useSystemConfigSources()` or `QuarkusComponentTestExtensionBuilder#useSystemConfigSources()` to configure this behavior.
305+
300306
== Mocking CDI Interceptors
301307

302308
If a tested component class declares an interceptor binding then you might need to mock the interception too.

test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,11 @@
8080
*/
8181
Class<? extends Converter<?>>[] configConverters() default {};
8282

83+
/**
84+
* If set to {@code true} then config sources for system properties and ENV variables are included in the test config.
85+
*
86+
* @see QuarkusComponentTestExtensionBuilder#useSystemConfigSources(boolean)
87+
*/
88+
boolean useSystemConfigSources() default false;
89+
8390
}

test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestConfiguration.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ class QuarkusComponentTestConfiguration {
6363

6464
static final QuarkusComponentTestConfiguration DEFAULT = new QuarkusComponentTestConfiguration(Map.of(), Set.of(),
6565
List.of(), false, true, QuarkusComponentTestExtensionBuilder.DEFAULT_CONFIG_SOURCE_ORDINAL, List.of(),
66-
DEFAULT_CONVERTERS, null);
66+
DEFAULT_CONVERTERS, null, false);
6767

6868
private static final Logger LOG = Logger.getLogger(QuarkusComponentTestConfiguration.class);
6969

7070
final Map<String, String> configProperties;
7171
final Set<Class<?>> componentClasses;
7272
final List<MockBeanConfiguratorImpl<?>> mockConfigurators;
7373
final boolean useDefaultConfigProperties;
74+
final boolean useSystemConfigSources;
7475
final boolean addNestedClassesAsComponents;
7576
final int configSourceOrdinal;
7677
final List<AnnotationsTransformer> annotationsTransformers;
@@ -81,11 +82,12 @@ class QuarkusComponentTestConfiguration {
8182
List<MockBeanConfiguratorImpl<?>> mockConfigurators, boolean useDefaultConfigProperties,
8283
boolean addNestedClassesAsComponents, int configSourceOrdinal,
8384
List<AnnotationsTransformer> annotationsTransformers, List<Converter<?>> configConverters,
84-
Consumer<SmallRyeConfigBuilder> configBuilderCustomizer) {
85+
Consumer<SmallRyeConfigBuilder> configBuilderCustomizer, boolean useSystemConfigSources) {
8586
this.configProperties = configProperties;
8687
this.componentClasses = componentClasses;
8788
this.mockConfigurators = mockConfigurators;
8889
this.useDefaultConfigProperties = useDefaultConfigProperties;
90+
this.useSystemConfigSources = useSystemConfigSources;
8991
this.addNestedClassesAsComponents = addNestedClassesAsComponents;
9092
this.configSourceOrdinal = configSourceOrdinal;
9193
this.annotationsTransformers = annotationsTransformers;
@@ -97,6 +99,7 @@ QuarkusComponentTestConfiguration update(Class<?> testClass) {
9799
Map<String, String> configProperties = new HashMap<>(this.configProperties);
98100
List<Class<?>> componentClasses = new ArrayList<>(this.componentClasses);
99101
boolean useDefaultConfigProperties = this.useDefaultConfigProperties;
102+
boolean useSystemConfigSources = this.useSystemConfigSources;
100103
boolean addNestedClassesAsComponents = this.addNestedClassesAsComponents;
101104
int configSourceOrdinal = this.configSourceOrdinal;
102105
List<AnnotationsTransformer> annotationsTransformers = new ArrayList<>(this.annotationsTransformers);
@@ -112,6 +115,7 @@ QuarkusComponentTestConfiguration update(Class<?> testClass) {
112115
if (testAnnotation != null) {
113116
Collections.addAll(componentClasses, testAnnotation.value());
114117
useDefaultConfigProperties = testAnnotation.useDefaultConfigProperties();
118+
useSystemConfigSources = testAnnotation.useSystemConfigSources();
115119
addNestedClassesAsComponents = testAnnotation.addNestedClassesAsComponents();
116120
configSourceOrdinal = testAnnotation.configSourceOrdinal();
117121
Class<? extends AnnotationsTransformer>[] transformers = testAnnotation.annotationsTransformers();
@@ -148,7 +152,8 @@ QuarkusComponentTestConfiguration update(Class<?> testClass) {
148152

149153
return new QuarkusComponentTestConfiguration(Map.copyOf(configProperties), Set.copyOf(componentClasses),
150154
this.mockConfigurators, useDefaultConfigProperties, addNestedClassesAsComponents, configSourceOrdinal,
151-
List.copyOf(annotationsTransformers), List.copyOf(configConverters), configBuilderCustomizer);
155+
List.copyOf(annotationsTransformers), List.copyOf(configConverters), configBuilderCustomizer,
156+
useSystemConfigSources);
152157
}
153158

154159
private static void collectComponents(Class<?> testClass, boolean addNestedClassesAsComponents,
@@ -220,7 +225,7 @@ QuarkusComponentTestConfiguration update(Method testMethod) {
220225
}
221226
return new QuarkusComponentTestConfiguration(configProperties, componentClasses,
222227
mockConfigurators, useDefaultConfigProperties, addNestedClassesAsComponents, configSourceOrdinal,
223-
annotationsTransformers, configConverters, configBuilderCustomizer);
228+
annotationsTransformers, configConverters, configBuilderCustomizer, useSystemConfigSources);
224229
}
225230

226231
private static boolean resolvesToBuiltinBean(Class<?> rawType) {

test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public QuarkusComponentTestExtension() {
212212
public QuarkusComponentTestExtension(Class<?>... additionalComponentClasses) {
213213
this(new QuarkusComponentTestConfiguration(Map.of(), Set.of(additionalComponentClasses),
214214
List.of(), false, true, QuarkusComponentTestExtensionBuilder.DEFAULT_CONFIG_SOURCE_ORDINAL,
215-
List.of(), List.of(), null), false);
215+
List.of(), List.of(), null, false), false);
216216
}
217217

218218
QuarkusComponentTestExtension(QuarkusComponentTestConfiguration baseConfiguration, boolean startShouldFail) {
@@ -506,10 +506,17 @@ private void startContainer(ExtensionContext context, Lifecycle testInstanceLife
506506
SmallRyeConfigBuilder configBuilder = new SmallRyeConfigBuilder().forClassLoader(tccl)
507507
.addDefaultInterceptors()
508508
.withConverters(configuration.configConverters.toArray(new Converter<?>[] {}))
509-
.addDefaultSources()
509+
// We intentionally skip system properties and ENV variables by default
510+
// See https://github.com/quarkusio/quarkus/issues/48899 for more details
511+
.addPropertiesSources()
510512
.withSources(
511513
new QuarkusComponentTestConfigSource(configuration.configProperties,
512514
configuration.configSourceOrdinal));
515+
516+
if (configuration.useSystemConfigSources) {
517+
configBuilder.addSystemSources();
518+
}
519+
513520
@SuppressWarnings("unchecked")
514521
Set<ConfigClass> configMappings = store(context).get(KEY_CONFIG_MAPPINGS, Set.class);
515522
if (configMappings != null) {

test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtensionBuilder.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class QuarkusComponentTestExtensionBuilder {
3434
private final List<AnnotationsTransformer> annotationsTransformers = new ArrayList<>();
3535
private final List<Converter<?>> configConverters = new ArrayList<>();
3636
private boolean useDefaultConfigProperties = false;
37+
private boolean useSystemConfigSources = false;
3738
private boolean addNestedClassesAsComponents = true;
3839
private int configSourceOrdinal = QuarkusComponentTestExtensionBuilder.DEFAULT_CONFIG_SOURCE_ORDINAL;
3940
private Consumer<SmallRyeConfigBuilder> configBuilderCustomizer;
@@ -139,6 +140,16 @@ public QuarkusComponentTestExtensionBuilder setConfigBuilderCustomizer(Consumer<
139140
return this;
140141
}
141142

143+
/**
144+
* Use config sources for system properties and ENV variables in the test config.
145+
*
146+
* @return self
147+
*/
148+
public QuarkusComponentTestExtensionBuilder useSystemConfigSources(boolean value) {
149+
this.useSystemConfigSources = value;
150+
return this;
151+
}
152+
142153
/**
143154
* Configure a new mock of a bean.
144155
* <p>
@@ -174,7 +185,8 @@ public QuarkusComponentTestExtension build() {
174185
return new QuarkusComponentTestExtension(new QuarkusComponentTestConfiguration(Map.copyOf(configProperties),
175186
Set.copyOf(componentClasses), List.copyOf(mockConfigurators), useDefaultConfigProperties,
176187
addNestedClassesAsComponents, configSourceOrdinal,
177-
List.copyOf(annotationsTransformers), converters, configBuilderCustomizer), buildShouldFail);
188+
List.copyOf(annotationsTransformers), converters, configBuilderCustomizer, useSystemConfigSources),
189+
buildShouldFail);
178190
}
179191

180192
void registerMockBean(MockBeanConfiguratorImpl<?> mock) {

test-framework/junit5-component/src/test/java/io/quarkus/test/component/config/ConfigSourceOrdinalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import io.quarkus.test.component.QuarkusComponentTest;
1414
import io.quarkus.test.component.TestConfigProperty;
1515

16-
@QuarkusComponentTest(configSourceOrdinal = 275)
16+
@QuarkusComponentTest(configSourceOrdinal = 275, useSystemConfigSources = true)
1717
@TestConfigProperty(key = "foo", value = "baz")
1818
public class ConfigSourceOrdinalTest {
1919

0 commit comments

Comments
 (0)