Streamline use of TestContextAnnotationUtils

See gh-12470
This commit is contained in:
Andy Wilkinson 2020-10-28 10:20:14 +00:00
parent d9084eab67
commit 506f7acc8d
8 changed files with 19 additions and 37 deletions

View File

@ -26,7 +26,6 @@ import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory; import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
/** /**
* {@link ContextCustomizerFactory} to support * {@link ContextCustomizerFactory} to support
@ -39,9 +38,9 @@ class OverrideAutoConfigurationContextCustomizerFactory implements ContextCustom
@Override @Override
public ContextCustomizer createContextCustomizer(Class<?> testClass, public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configurationAttributes) { List<ContextConfigurationAttributes> configurationAttributes) {
AnnotationDescriptor<OverrideAutoConfiguration> descriptor = TestContextAnnotationUtils OverrideAutoConfiguration overrideAutoConfiguration = TestContextAnnotationUtils.findMergedAnnotation(testClass,
.findAnnotationDescriptor(testClass, OverrideAutoConfiguration.class); OverrideAutoConfiguration.class);
boolean enabled = (descriptor != null) ? descriptor.getAnnotation().enabled() : true; boolean enabled = (overrideAutoConfiguration != null) ? overrideAutoConfiguration.enabled() : true;
return !enabled ? new DisableAutoConfigurationContextCustomizer() : null; return !enabled ? new DisableAutoConfigurationContextCustomizer() : null;
} }

View File

@ -37,7 +37,7 @@ class MetricsExportContextCustomizerFactory implements ContextCustomizerFactory
@Override @Override
public ContextCustomizer createContextCustomizer(Class<?> testClass, public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) { List<ContextConfigurationAttributes> configAttributes) {
boolean disableMetricsExport = TestContextAnnotationUtils.findAnnotationDescriptor(testClass, boolean disableMetricsExport = TestContextAnnotationUtils.findMergedAnnotation(testClass,
AutoConfigureMetrics.class) == null; AutoConfigureMetrics.class) == null;
return disableMetricsExport ? new DisableMetricExportContextCustomizer() : null; return disableMetricsExport ? new DisableMetricExportContextCustomizer() : null;
} }

View File

@ -47,7 +47,6 @@ import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.context.TestContextBootstrapper; import org.springframework.test.context.TestContextBootstrapper;
import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.DefaultTestContextBootstrapper; import org.springframework.test.context.support.DefaultTestContextBootstrapper;
@ -320,12 +319,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
} }
protected SpringBootTest getAnnotation(Class<?> testClass) { protected SpringBootTest getAnnotation(Class<?> testClass) {
AnnotationDescriptor<SpringBootTest> descriptor = TestContextAnnotationUtils.findAnnotationDescriptor(testClass, return TestContextAnnotationUtils.findMergedAnnotation(testClass, SpringBootTest.class);
SpringBootTest.class);
if (descriptor != null) {
return descriptor.getAnnotation();
}
return null;
} }
protected void verifyConfiguration(Class<?> testClass) { protected void verifyConfiguration(Class<?> testClass) {

View File

@ -21,7 +21,6 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextCustomizer; import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
/** /**
* {@link ContextCustomizer} to track the web environment that is used in a * {@link ContextCustomizer} to track the web environment that is used in a
@ -36,9 +35,9 @@ class SpringBootTestWebEnvironment implements ContextCustomizer {
private final WebEnvironment webEnvironment; private final WebEnvironment webEnvironment;
SpringBootTestWebEnvironment(Class<?> testClass) { SpringBootTestWebEnvironment(Class<?> testClass) {
AnnotationDescriptor<SpringBootTest> descriptor = TestContextAnnotationUtils.findAnnotationDescriptor(testClass, SpringBootTest sprintBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
SpringBootTest.class); SpringBootTest.class);
this.webEnvironment = (descriptor != null) ? descriptor.getAnnotation().webEnvironment() : null; this.webEnvironment = (sprintBootTest != null) ? sprintBootTest.webEnvironment() : null;
} }
@Override @Override

View File

@ -40,7 +40,6 @@ import org.springframework.core.Ordered;
import org.springframework.test.context.ContextCustomizer; import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
/** /**
* {@link ContextCustomizer} for {@link TestRestTemplate}. * {@link ContextCustomizer} for {@link TestRestTemplate}.
@ -53,9 +52,9 @@ class TestRestTemplateContextCustomizer implements ContextCustomizer {
@Override @Override
public void customizeContext(ConfigurableApplicationContext context, public void customizeContext(ConfigurableApplicationContext context,
MergedContextConfiguration mergedContextConfiguration) { MergedContextConfiguration mergedContextConfiguration) {
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils SpringBootTest springBootTest = TestContextAnnotationUtils
.findAnnotationDescriptor(mergedContextConfiguration.getTestClass(), SpringBootTest.class); .findMergedAnnotation(mergedContextConfiguration.getTestClass(), SpringBootTest.class);
if (springBootTest.getAnnotation().webEnvironment().isEmbedded()) { if (springBootTest.webEnvironment().isEmbedded()) {
registerTestRestTemplate(context); registerTestRestTemplate(context);
} }
} }

View File

@ -23,7 +23,6 @@ import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer; import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory; import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
/** /**
* {@link ContextCustomizerFactory} for {@link TestRestTemplate}. * {@link ContextCustomizerFactory} for {@link TestRestTemplate}.
@ -36,12 +35,9 @@ class TestRestTemplateContextCustomizerFactory implements ContextCustomizerFacto
@Override @Override
public ContextCustomizer createContextCustomizer(Class<?> testClass, public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) { List<ContextConfigurationAttributes> configAttributes) {
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
.findAnnotationDescriptor(testClass, SpringBootTest.class); SpringBootTest.class);
if (springBootTest != null) { return (springBootTest != null) ? new TestRestTemplateContextCustomizer() : null;
return new TestRestTemplateContextCustomizer();
}
return null;
} }
} }

View File

@ -41,7 +41,6 @@ import org.springframework.core.Ordered;
import org.springframework.test.context.ContextCustomizer; import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.client.ExchangeStrategies; import org.springframework.web.reactive.function.client.ExchangeStrategies;
@ -55,9 +54,9 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
@Override @Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(mergedConfig.getTestClass(),
.findAnnotationDescriptor(mergedConfig.getTestClass(), SpringBootTest.class); SpringBootTest.class);
if (springBootTest.getAnnotation().webEnvironment().isEmbedded()) { if (springBootTest.webEnvironment().isEmbedded()) {
registerWebTestClient(context); registerWebTestClient(context);
} }
} }

View File

@ -23,7 +23,6 @@ import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer; import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory; import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
@ -38,12 +37,9 @@ class WebTestClientContextCustomizerFactory implements ContextCustomizerFactory
@Override @Override
public ContextCustomizer createContextCustomizer(Class<?> testClass, public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) { List<ContextConfigurationAttributes> configAttributes) {
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
.findAnnotationDescriptor(testClass, SpringBootTest.class); SpringBootTest.class);
if (springBootTest != null) { return (springBootTest != null) ? new WebTestClientContextCustomizer() : null;
return new WebTestClientContextCustomizer();
}
return null;
} }
private boolean isWebClientPresent() { private boolean isWebClientPresent() {