Skip to content

Commit

Permalink
Streamline use of TestContextAnnotationUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Oct 28, 2020
1 parent d9084ea commit 506f7ac
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 37 deletions.
Expand Up @@ -26,7 +26,6 @@
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
Expand All @@ -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;
}

Expand Down
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -47,7 +47,6 @@
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;
Expand Down Expand Up @@ -320,12 +319,7 @@ protected String[] getProperties(Class<?> testClass) {
}

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) {
Expand Down
Expand Up @@ -21,7 +21,6 @@
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
Expand All @@ -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
Expand Down
Expand Up @@ -40,7 +40,6 @@
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}.
Expand All @@ -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);
}
}
Expand Down
Expand Up @@ -23,7 +23,6 @@
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}.
Expand All @@ -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;
}

}
Expand Up @@ -41,7 +41,6 @@
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;
Expand All @@ -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);
}
}
Expand Down
Expand Up @@ -23,7 +23,6 @@
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;

/**
Expand All @@ -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() {
Expand Down

0 comments on commit 506f7ac

Please sign in to comment.