Discover @DynamicPropertySource methods on enclosing test classes

gh-19930 introduced support for finding class-level test configuration
annotations on enclosing classes when using JUnit Jupiter @Nested test
classes, but support for @DynamicPropertySource methods got overlooked
since they are method-level annotations.

This commit addresses this shortcoming by introducing full support for
@NestedTestConfiguration semantics for @DynamicPropertySource methods
on enclosing classes.

Closes gh-26091
This commit is contained in:
Sam Brannen
2020-11-15 20:59:15 +01:00
parent 0b580d194d
commit 0819a9fcc9
5 changed files with 204 additions and 1 deletions

View File

@@ -41,6 +41,11 @@ import java.lang.annotation.Target;
* is resolved. Typically, method references are used to supply values, as in the
* example below.
*
* <p>As of Spring Framework 5.3.2, dynamic properties from methods annotated with
* {@code @DynamicPropertySource} will be <em>inherited</em> from enclosing test
* classes, analogous to inheritance from superclasses and interfaces. See
* {@link NestedTestConfiguration @NestedTestConfiguration} for details.
*
* <p><strong>NOTE</strong>: if you use {@code @DynamicPropertySource} in a base
* class and discover that tests in subclasses fail because the dynamic properties
* change between subclasses, you may need to annotate your base class with

View File

@@ -74,6 +74,7 @@ import org.springframework.lang.Nullable;
* <li>{@link org.springframework.test.context.web.WebAppConfiguration @WebAppConfiguration}</li>
* <li>{@link ActiveProfiles @ActiveProfiles}</li>
* <li>{@link TestPropertySource @TestPropertySource}</li>
* <li>{@link DynamicPropertySource @DynamicPropertySource}</li>
* <li>{@link org.springframework.test.annotation.DirtiesContext @DirtiesContext}</li>
* <li>{@link org.springframework.transaction.annotation.Transactional @Transactional}</li>
* <li>{@link org.springframework.test.annotation.Rollback @Rollback}</li>

View File

@@ -17,6 +17,7 @@
package org.springframework.test.context.support;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -26,12 +27,14 @@ import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.TestContextAnnotationUtils;
/**
* {@link ContextCustomizerFactory} to support
* {@link DynamicPropertySource @DynamicPropertySource} methods.
*
* @author Phillip Webb
* @author Sam Brannen
* @since 5.2.5
* @see DynamicPropertiesContextCustomizer
*/
@@ -42,13 +45,21 @@ class DynamicPropertiesContextCustomizerFactory implements ContextCustomizerFact
public DynamicPropertiesContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
Set<Method> methods = MethodIntrospector.selectMethods(testClass, this::isAnnotated);
Set<Method> methods = new LinkedHashSet<>();
findMethods(testClass, methods);
if (methods.isEmpty()) {
return null;
}
return new DynamicPropertiesContextCustomizer(methods);
}
private void findMethods(Class<?> testClass, Set<Method> methods) {
methods.addAll(MethodIntrospector.selectMethods(testClass, this::isAnnotated));
if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) {
findMethods(testClass.getEnclosingClass(), methods);
}
}
private boolean isAnnotated(Method method) {
return MergedAnnotations.from(method).isPresent(DynamicPropertySource.class);
}