Skip to content

Commit 502ccb6

Browse files
committed
Honor spring.autoconfigure.exclude in test slices
Previously, the import selector for `@ImportAutoConfiguration` did not consider the spring.autoconfigure.exclude property when determining which auto-configurations to exclude. This meant that tests using a slice that included a particular auto-configuration would include it even if the application's configuration excluded it via spring.autoconfigure.exclude. Confusingly, this could result in a sliced test using an auto-configuration that would be excluded in a broader `@SpringBootTest`. This commit updates the ImportAutoConfigurationImportSelector to consider the spring.autoconfigure.exclude property so that sliced tests will use a subset of the auto-configurations that a `@SpringBootTest` would use. Fixes gh-21736
1 parent 1b85ce0 commit 502ccb6

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,23 @@ protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttri
233233
return excluded;
234234
}
235235

236-
private List<String> getExcludeAutoConfigurationsProperty() {
237-
if (getEnvironment() instanceof ConfigurableEnvironment) {
238-
Binder binder = Binder.get(getEnvironment());
236+
/**
237+
* Returns the auto-configurations excluded by the
238+
* {@code spring.autoconfigure.exclude} property.
239+
* @return excluded auto-configurations
240+
* @since 2.3.2
241+
*/
242+
protected List<String> getExcludeAutoConfigurationsProperty() {
243+
Environment environment = getEnvironment();
244+
if (environment == null) {
245+
return Collections.emptyList();
246+
}
247+
if (environment instanceof ConfigurableEnvironment) {
248+
Binder binder = Binder.get(environment);
239249
return binder.bind(PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String[].class).map(Arrays::asList)
240250
.orElse(Collections.emptyList());
241251
}
242-
String[] excludes = getEnvironment().getProperty(PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String[].class);
252+
String[] excludes = environment.getProperty(PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String[].class);
243253
return (excludes != null) ? Arrays.asList(excludes) : Collections.emptyList();
244254
}
245255

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -118,6 +118,7 @@ protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttri
118118
}
119119
}
120120
}
121+
exclusions.addAll(getExcludeAutoConfigurationsProperty());
121122
return exclusions;
122123
}
123124

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,19 @@
2525

2626
import org.junit.jupiter.api.BeforeEach;
2727
import org.junit.jupiter.api.Test;
28-
import org.mockito.Mock;
29-
import org.mockito.MockitoAnnotations;
3028

3129
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3230
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3331
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
3432
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
3533
import org.springframework.core.annotation.AliasFor;
36-
import org.springframework.core.env.Environment;
3734
import org.springframework.core.io.DefaultResourceLoader;
3835
import org.springframework.core.type.AnnotationMetadata;
3936
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
37+
import org.springframework.mock.env.MockEnvironment;
4038
import org.springframework.util.ClassUtils;
4139

4240
import static org.assertj.core.api.Assertions.assertThat;
43-
import static org.mockito.Mockito.verifyNoInteractions;
4441

4542
/**
4643
* Tests for {@link ImportAutoConfigurationImportSelector}.
@@ -54,12 +51,10 @@ class ImportAutoConfigurationImportSelectorTests {
5451

5552
private final ConfigurableListableBeanFactory beanFactory = new DefaultListableBeanFactory();
5653

57-
@Mock
58-
private Environment environment;
54+
private final MockEnvironment environment = new MockEnvironment();
5955

6056
@BeforeEach
6157
void setup() {
62-
MockitoAnnotations.initMocks(this);
6358
this.importSelector.setBeanFactory(this.beanFactory);
6459
this.importSelector.setEnvironment(this.environment);
6560
this.importSelector.setResourceLoader(new DefaultResourceLoader());
@@ -80,10 +75,11 @@ void importsAreSelectedUsingClassesAttribute() throws Exception {
8075
}
8176

8277
@Test
83-
void propertyExclusionsAreNotApplied() throws Exception {
84-
AnnotationMetadata annotationMetadata = getAnnotationMetadata(ImportFreeMarker.class);
85-
this.importSelector.selectImports(annotationMetadata);
86-
verifyNoInteractions(this.environment);
78+
void propertyExclusionsAreApplied() throws IOException {
79+
this.environment.setProperty("spring.autoconfigure.exclude", FreeMarkerAutoConfiguration.class.getName());
80+
AnnotationMetadata annotationMetadata = getAnnotationMetadata(MultipleImports.class);
81+
String[] imports = this.importSelector.selectImports(annotationMetadata);
82+
assertThat(imports).containsExactly(ThymeleafAutoConfiguration.class.getName());
8783
}
8884

8985
@Test
@@ -288,7 +284,9 @@ static class ImportMetaAutoConfigurationExcludeWithUnrelatedTwo {
288284
@interface MetaImportAutoConfiguration {
289285

290286
@AliasFor(annotation = ImportAutoConfiguration.class)
291-
Class<?>[] exclude() default {};
287+
Class<?>[] exclude() default {
288+
289+
};
292290

293291
}
294292

@@ -308,7 +306,9 @@ static class ImportMetaAutoConfigurationExcludeWithUnrelatedTwo {
308306
@interface SelfAnnotating {
309307

310308
@AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
311-
Class<?>[] excludeAutoConfiguration() default {};
309+
Class<?>[] excludeAutoConfiguration() default {
310+
311+
};
312312

313313
}
314314

0 commit comments

Comments
 (0)