Skip to content

Commit 506f7ac

Browse files
committed
Streamline use of TestContextAnnotationUtils
See gh-12470
1 parent d9084ea commit 506f7ac

File tree

8 files changed

+19
-37
lines changed

8 files changed

+19
-37
lines changed

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.test.context.ContextCustomizerFactory;
2727
import org.springframework.test.context.MergedContextConfiguration;
2828
import org.springframework.test.context.TestContextAnnotationUtils;
29-
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
3029

3130
/**
3231
* {@link ContextCustomizerFactory} to support
@@ -39,9 +38,9 @@ class OverrideAutoConfigurationContextCustomizerFactory implements ContextCustom
3938
@Override
4039
public ContextCustomizer createContextCustomizer(Class<?> testClass,
4140
List<ContextConfigurationAttributes> configurationAttributes) {
42-
AnnotationDescriptor<OverrideAutoConfiguration> descriptor = TestContextAnnotationUtils
43-
.findAnnotationDescriptor(testClass, OverrideAutoConfiguration.class);
44-
boolean enabled = (descriptor != null) ? descriptor.getAnnotation().enabled() : true;
41+
OverrideAutoConfiguration overrideAutoConfiguration = TestContextAnnotationUtils.findMergedAnnotation(testClass,
42+
OverrideAutoConfiguration.class);
43+
boolean enabled = (overrideAutoConfiguration != null) ? overrideAutoConfiguration.enabled() : true;
4544
return !enabled ? new DisableAutoConfigurationContextCustomizer() : null;
4645
}
4746

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/actuate/metrics/MetricsExportContextCustomizerFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class MetricsExportContextCustomizerFactory implements ContextCustomizerFactory
3737
@Override
3838
public ContextCustomizer createContextCustomizer(Class<?> testClass,
3939
List<ContextConfigurationAttributes> configAttributes) {
40-
boolean disableMetricsExport = TestContextAnnotationUtils.findAnnotationDescriptor(testClass,
40+
boolean disableMetricsExport = TestContextAnnotationUtils.findMergedAnnotation(testClass,
4141
AutoConfigureMetrics.class) == null;
4242
return disableMetricsExport ? new DisableMetricExportContextCustomizer() : null;
4343
}

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.springframework.test.context.MergedContextConfiguration;
4848
import org.springframework.test.context.TestContext;
4949
import org.springframework.test.context.TestContextAnnotationUtils;
50-
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
5150
import org.springframework.test.context.TestContextBootstrapper;
5251
import org.springframework.test.context.TestExecutionListener;
5352
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
@@ -320,12 +319,7 @@ protected String[] getProperties(Class<?> testClass) {
320319
}
321320

322321
protected SpringBootTest getAnnotation(Class<?> testClass) {
323-
AnnotationDescriptor<SpringBootTest> descriptor = TestContextAnnotationUtils.findAnnotationDescriptor(testClass,
324-
SpringBootTest.class);
325-
if (descriptor != null) {
326-
return descriptor.getAnnotation();
327-
}
328-
return null;
322+
return TestContextAnnotationUtils.findMergedAnnotation(testClass, SpringBootTest.class);
329323
}
330324

331325
protected void verifyConfiguration(Class<?> testClass) {

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.springframework.test.context.ContextCustomizer;
2222
import org.springframework.test.context.MergedContextConfiguration;
2323
import org.springframework.test.context.TestContextAnnotationUtils;
24-
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
2524

2625
/**
2726
* {@link ContextCustomizer} to track the web environment that is used in a
@@ -36,9 +35,9 @@ class SpringBootTestWebEnvironment implements ContextCustomizer {
3635
private final WebEnvironment webEnvironment;
3736

3837
SpringBootTestWebEnvironment(Class<?> testClass) {
39-
AnnotationDescriptor<SpringBootTest> descriptor = TestContextAnnotationUtils.findAnnotationDescriptor(testClass,
38+
SpringBootTest sprintBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
4039
SpringBootTest.class);
41-
this.webEnvironment = (descriptor != null) ? descriptor.getAnnotation().webEnvironment() : null;
40+
this.webEnvironment = (sprintBootTest != null) ? sprintBootTest.webEnvironment() : null;
4241
}
4342

4443
@Override

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizer.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.springframework.test.context.ContextCustomizer;
4141
import org.springframework.test.context.MergedContextConfiguration;
4242
import org.springframework.test.context.TestContextAnnotationUtils;
43-
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
4443

4544
/**
4645
* {@link ContextCustomizer} for {@link TestRestTemplate}.
@@ -53,9 +52,9 @@ class TestRestTemplateContextCustomizer implements ContextCustomizer {
5352
@Override
5453
public void customizeContext(ConfigurableApplicationContext context,
5554
MergedContextConfiguration mergedContextConfiguration) {
56-
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils
57-
.findAnnotationDescriptor(mergedContextConfiguration.getTestClass(), SpringBootTest.class);
58-
if (springBootTest.getAnnotation().webEnvironment().isEmbedded()) {
55+
SpringBootTest springBootTest = TestContextAnnotationUtils
56+
.findMergedAnnotation(mergedContextConfiguration.getTestClass(), SpringBootTest.class);
57+
if (springBootTest.webEnvironment().isEmbedded()) {
5958
registerTestRestTemplate(context);
6059
}
6160
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizerFactory.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.springframework.test.context.ContextCustomizer;
2424
import org.springframework.test.context.ContextCustomizerFactory;
2525
import org.springframework.test.context.TestContextAnnotationUtils;
26-
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
2726

2827
/**
2928
* {@link ContextCustomizerFactory} for {@link TestRestTemplate}.
@@ -36,12 +35,9 @@ class TestRestTemplateContextCustomizerFactory implements ContextCustomizerFacto
3635
@Override
3736
public ContextCustomizer createContextCustomizer(Class<?> testClass,
3837
List<ContextConfigurationAttributes> configAttributes) {
39-
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils
40-
.findAnnotationDescriptor(testClass, SpringBootTest.class);
41-
if (springBootTest != null) {
42-
return new TestRestTemplateContextCustomizer();
43-
}
44-
return null;
38+
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
39+
SpringBootTest.class);
40+
return (springBootTest != null) ? new TestRestTemplateContextCustomizer() : null;
4541
}
4642

4743
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/server/WebTestClientContextCustomizer.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.springframework.test.context.ContextCustomizer;
4242
import org.springframework.test.context.MergedContextConfiguration;
4343
import org.springframework.test.context.TestContextAnnotationUtils;
44-
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
4544
import org.springframework.test.web.reactive.server.WebTestClient;
4645
import org.springframework.util.CollectionUtils;
4746
import org.springframework.web.reactive.function.client.ExchangeStrategies;
@@ -55,9 +54,9 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
5554

5655
@Override
5756
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
58-
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils
59-
.findAnnotationDescriptor(mergedConfig.getTestClass(), SpringBootTest.class);
60-
if (springBootTest.getAnnotation().webEnvironment().isEmbedded()) {
57+
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(mergedConfig.getTestClass(),
58+
SpringBootTest.class);
59+
if (springBootTest.webEnvironment().isEmbedded()) {
6160
registerWebTestClient(context);
6261
}
6362
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/server/WebTestClientContextCustomizerFactory.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.springframework.test.context.ContextCustomizer;
2424
import org.springframework.test.context.ContextCustomizerFactory;
2525
import org.springframework.test.context.TestContextAnnotationUtils;
26-
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
2726
import org.springframework.util.ClassUtils;
2827

2928
/**
@@ -38,12 +37,9 @@ class WebTestClientContextCustomizerFactory implements ContextCustomizerFactory
3837
@Override
3938
public ContextCustomizer createContextCustomizer(Class<?> testClass,
4039
List<ContextConfigurationAttributes> configAttributes) {
41-
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils
42-
.findAnnotationDescriptor(testClass, SpringBootTest.class);
43-
if (springBootTest != null) {
44-
return new WebTestClientContextCustomizer();
45-
}
46-
return null;
40+
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
41+
SpringBootTest.class);
42+
return (springBootTest != null) ? new WebTestClientContextCustomizer() : null;
4743
}
4844

4945
private boolean isWebClientPresent() {

0 commit comments

Comments
 (0)