Streamline use of TestContextAnnotationUtils
See gh-12470
This commit is contained in:
parent
d9084eab67
commit
506f7acc8d
|
|
@ -26,7 +26,6 @@ import org.springframework.test.context.ContextCustomizer;
|
|||
import org.springframework.test.context.ContextCustomizerFactory;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
|
||||
|
||||
/**
|
||||
* {@link ContextCustomizerFactory} to support
|
||||
|
|
@ -39,9 +38,9 @@ class OverrideAutoConfigurationContextCustomizerFactory implements ContextCustom
|
|||
@Override
|
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
||||
List<ContextConfigurationAttributes> configurationAttributes) {
|
||||
AnnotationDescriptor<OverrideAutoConfiguration> descriptor = TestContextAnnotationUtils
|
||||
.findAnnotationDescriptor(testClass, OverrideAutoConfiguration.class);
|
||||
boolean enabled = (descriptor != null) ? descriptor.getAnnotation().enabled() : true;
|
||||
OverrideAutoConfiguration overrideAutoConfiguration = TestContextAnnotationUtils.findMergedAnnotation(testClass,
|
||||
OverrideAutoConfiguration.class);
|
||||
boolean enabled = (overrideAutoConfiguration != null) ? overrideAutoConfiguration.enabled() : true;
|
||||
return !enabled ? new DisableAutoConfigurationContextCustomizer() : null;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class MetricsExportContextCustomizerFactory implements ContextCustomizerFactory
|
|||
@Override
|
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
||||
List<ContextConfigurationAttributes> configAttributes) {
|
||||
boolean disableMetricsExport = TestContextAnnotationUtils.findAnnotationDescriptor(testClass,
|
||||
boolean disableMetricsExport = TestContextAnnotationUtils.findMergedAnnotation(testClass,
|
||||
AutoConfigureMetrics.class) == null;
|
||||
return disableMetricsExport ? new DisableMetricExportContextCustomizer() : null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,6 @@ import org.springframework.test.context.ContextLoader;
|
|||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
|
||||
import org.springframework.test.context.TestContextBootstrapper;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
|
||||
|
|
@ -320,12 +319,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
|
|||
}
|
||||
|
||||
protected SpringBootTest getAnnotation(Class<?> testClass) {
|
||||
AnnotationDescriptor<SpringBootTest> descriptor = TestContextAnnotationUtils.findAnnotationDescriptor(testClass,
|
||||
SpringBootTest.class);
|
||||
if (descriptor != null) {
|
||||
return descriptor.getAnnotation();
|
||||
}
|
||||
return null;
|
||||
return TestContextAnnotationUtils.findMergedAnnotation(testClass, SpringBootTest.class);
|
||||
}
|
||||
|
||||
protected void verifyConfiguration(Class<?> testClass) {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
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
|
||||
|
|
@ -36,9 +35,9 @@ class SpringBootTestWebEnvironment implements ContextCustomizer {
|
|||
private final WebEnvironment webEnvironment;
|
||||
|
||||
SpringBootTestWebEnvironment(Class<?> testClass) {
|
||||
AnnotationDescriptor<SpringBootTest> descriptor = TestContextAnnotationUtils.findAnnotationDescriptor(testClass,
|
||||
SpringBootTest sprintBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
|
||||
SpringBootTest.class);
|
||||
this.webEnvironment = (descriptor != null) ? descriptor.getAnnotation().webEnvironment() : null;
|
||||
this.webEnvironment = (sprintBootTest != null) ? sprintBootTest.webEnvironment() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ import org.springframework.core.Ordered;
|
|||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
|
||||
|
||||
/**
|
||||
* {@link ContextCustomizer} for {@link TestRestTemplate}.
|
||||
|
|
@ -53,9 +52,9 @@ class TestRestTemplateContextCustomizer implements ContextCustomizer {
|
|||
@Override
|
||||
public void customizeContext(ConfigurableApplicationContext context,
|
||||
MergedContextConfiguration mergedContextConfiguration) {
|
||||
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils
|
||||
.findAnnotationDescriptor(mergedContextConfiguration.getTestClass(), SpringBootTest.class);
|
||||
if (springBootTest.getAnnotation().webEnvironment().isEmbedded()) {
|
||||
SpringBootTest springBootTest = TestContextAnnotationUtils
|
||||
.findMergedAnnotation(mergedContextConfiguration.getTestClass(), SpringBootTest.class);
|
||||
if (springBootTest.webEnvironment().isEmbedded()) {
|
||||
registerTestRestTemplate(context);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ import org.springframework.test.context.ContextConfigurationAttributes;
|
|||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.ContextCustomizerFactory;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
|
||||
|
||||
/**
|
||||
* {@link ContextCustomizerFactory} for {@link TestRestTemplate}.
|
||||
|
|
@ -36,12 +35,9 @@ class TestRestTemplateContextCustomizerFactory implements ContextCustomizerFacto
|
|||
@Override
|
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
||||
List<ContextConfigurationAttributes> configAttributes) {
|
||||
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils
|
||||
.findAnnotationDescriptor(testClass, SpringBootTest.class);
|
||||
if (springBootTest != null) {
|
||||
return new TestRestTemplateContextCustomizer();
|
||||
}
|
||||
return null;
|
||||
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
|
||||
SpringBootTest.class);
|
||||
return (springBootTest != null) ? new TestRestTemplateContextCustomizer() : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ import org.springframework.core.Ordered;
|
|||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.reactive.function.client.ExchangeStrategies;
|
||||
|
|
@ -55,9 +54,9 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
|
|||
|
||||
@Override
|
||||
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
|
||||
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils
|
||||
.findAnnotationDescriptor(mergedConfig.getTestClass(), SpringBootTest.class);
|
||||
if (springBootTest.getAnnotation().webEnvironment().isEmbedded()) {
|
||||
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(mergedConfig.getTestClass(),
|
||||
SpringBootTest.class);
|
||||
if (springBootTest.webEnvironment().isEmbedded()) {
|
||||
registerWebTestClient(context);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ import org.springframework.test.context.ContextConfigurationAttributes;
|
|||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.ContextCustomizerFactory;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -38,12 +37,9 @@ class WebTestClientContextCustomizerFactory implements ContextCustomizerFactory
|
|||
@Override
|
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
||||
List<ContextConfigurationAttributes> configAttributes) {
|
||||
AnnotationDescriptor<SpringBootTest> springBootTest = TestContextAnnotationUtils
|
||||
.findAnnotationDescriptor(testClass, SpringBootTest.class);
|
||||
if (springBootTest != null) {
|
||||
return new WebTestClientContextCustomizer();
|
||||
}
|
||||
return null;
|
||||
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
|
||||
SpringBootTest.class);
|
||||
return (springBootTest != null) ? new WebTestClientContextCustomizer() : null;
|
||||
}
|
||||
|
||||
private boolean isWebClientPresent() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue