Skip to content

Commit 0f325f9

Browse files
committed
Disable ContextCustomizer implementations at runtime if necessary
This commit disables ContextCustomizer implementations that tune a test configuration context at runtime. Previously, these ran again and required additional hints to work properly. Rather than contributing those hints, the customizer is skipped as its impact is irrelevant in an AOT-optimized context: the context is fully prepared and the updates on the MergedContextConfiguration are not taken into account. Closes gh-32422
1 parent 2fe3054 commit 0f325f9

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideAutoConfigurationContextCustomizerFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020

21+
import org.springframework.aot.AotDetector;
2122
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2223
import org.springframework.boot.test.util.TestPropertyValues;
2324
import org.springframework.context.ConfigurableApplicationContext;
@@ -38,6 +39,9 @@ class OverrideAutoConfigurationContextCustomizerFactory implements ContextCustom
3839
@Override
3940
public ContextCustomizer createContextCustomizer(Class<?> testClass,
4041
List<ContextConfigurationAttributes> configurationAttributes) {
42+
if (AotDetector.useGeneratedArtifacts()) {
43+
return null;
44+
}
4145
OverrideAutoConfiguration overrideAutoConfiguration = TestContextAnnotationUtils.findMergedAnnotation(testClass,
4246
OverrideAutoConfiguration.class);
4347
boolean enabled = (overrideAutoConfiguration != null) ? overrideAutoConfiguration.enabled() : true;

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/filter/TypeExcludeFiltersContextCustomizerFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.LinkedHashSet;
2121
import java.util.List;
2222

23+
import org.springframework.aot.AotDetector;
2324
import org.springframework.boot.context.TypeExcludeFilter;
2425
import org.springframework.test.context.ContextConfigurationAttributes;
2526
import org.springframework.test.context.ContextCustomizer;
@@ -42,6 +43,9 @@ class TypeExcludeFiltersContextCustomizerFactory implements ContextCustomizerFac
4243
@Override
4344
public ContextCustomizer createContextCustomizer(Class<?> testClass,
4445
List<ContextConfigurationAttributes> configurationAttributes) {
46+
if (AotDetector.useGeneratedArtifacts()) {
47+
return null;
48+
}
4549
AnnotationDescriptor<TypeExcludeFilters> descriptor = TestContextAnnotationUtils
4650
.findAnnotationDescriptor(testClass, TypeExcludeFilters.class);
4751
Class<?>[] filterClasses = (descriptor != null) ? descriptor.getAnnotation().value() : NO_FILTERS;

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizerFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Method;
2020
import java.util.List;
2121

22+
import org.springframework.aot.AotDetector;
2223
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Import;
2425
import org.springframework.core.annotation.MergedAnnotations;
@@ -42,6 +43,9 @@ class ImportsContextCustomizerFactory implements ContextCustomizerFactory {
4243
@Override
4344
public ContextCustomizer createContextCustomizer(Class<?> testClass,
4445
List<ContextConfigurationAttributes> configAttributes) {
46+
if (AotDetector.useGeneratedArtifacts()) {
47+
return null;
48+
}
4549
AnnotationDescriptor<Import> descriptor = TestContextAnnotationUtils.findAnnotationDescriptor(testClass,
4650
Import.class);
4751
if (descriptor != null) {

0 commit comments

Comments
 (0)