Inherit enclosing class's configuration in nested tests

Fixes gh-12470
This commit is contained in:
Andy Wilkinson
2020-10-28 07:03:39 +00:00
parent b0a1c2a740
commit 6b437ece54
11 changed files with 171 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,12 +21,12 @@ import java.util.List;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.test.context.ContextConfigurationAttributes;
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,8 +39,9 @@ class OverrideAutoConfigurationContextCustomizerFactory implements ContextCustom
@Override
public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configurationAttributes) {
boolean enabled = MergedAnnotations.from(testClass, SearchStrategy.TYPE_HIERARCHY)
.get(OverrideAutoConfiguration.class).getValue("enabled", Boolean.class).orElse(true);
AnnotationDescriptor<OverrideAutoConfiguration> descriptor = TestContextAnnotationUtils
.findAnnotationDescriptor(testClass, OverrideAutoConfiguration.class);
boolean enabled = (descriptor != null) ? descriptor.getAnnotation().enabled() : true;
return !enabled ? new DisableAutoConfigurationContextCustomizer() : null;
}

View File

@@ -20,12 +20,11 @@ import java.util.List;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils;
/**
* {@link ContextCustomizerFactory} that globally disables metrics export unless
@@ -38,8 +37,8 @@ class MetricsExportContextCustomizerFactory implements ContextCustomizerFactory
@Override
public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
boolean disableMetricsExport = !MergedAnnotations.from(testClass, SearchStrategy.TYPE_HIERARCHY)
.get(AutoConfigureMetrics.class).isPresent();
boolean disableMetricsExport = TestContextAnnotationUtils.findAnnotationDescriptor(testClass,
AutoConfigureMetrics.class) == null;
return disableMetricsExport ? new DisableMetricExportContextCustomizer() : null;
}