Browse Source

Allow overriding dynamic property from enclosing class in nested test class

Prior to this commit, a dynamic property registered via a
@DynamicPropertySource method in a @Nested test class was not able to
override a property registered via a @DynamicPropertySource method in
the enclosing class.

See gh-26091
Closes gh-31083
pull/31496/head
Yanming Zhou 2 years ago committed by Sam Brannen
parent
commit
368036cab4
  1. 3
      spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java
  2. 17
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java

3
spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java

@ -35,6 +35,7 @@ import org.springframework.test.context.TestContextAnnotationUtils;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Sam Brannen * @author Sam Brannen
* @author Yanming Zhou
* @since 5.2.5 * @since 5.2.5
* @see DynamicPropertiesContextCustomizer * @see DynamicPropertiesContextCustomizer
*/ */
@ -54,10 +55,10 @@ class DynamicPropertiesContextCustomizerFactory implements ContextCustomizerFact
} }
private void findMethods(Class<?> testClass, Set<Method> methods) { private void findMethods(Class<?> testClass, Set<Method> methods) {
methods.addAll(MethodIntrospector.selectMethods(testClass, this::isAnnotated));
if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) { if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) {
findMethods(testClass.getEnclosingClass(), methods); findMethods(testClass.getEnclosingClass(), methods);
} }
methods.addAll(MethodIntrospector.selectMethods(testClass, this::isAnnotated));
} }
private boolean isAnnotated(Method method) { private boolean isAnnotated(Method method) {

17
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java

@ -39,6 +39,7 @@ import static org.springframework.test.context.NestedTestConfiguration.Enclosing
* {@link SpringExtension} in a JUnit Jupiter environment. * {@link SpringExtension} in a JUnit Jupiter environment.
* *
* @author Sam Brannen * @author Sam Brannen
* @author Yanming Zhou
* @since 5.3.2 * @since 5.3.2
*/ */
@SpringJUnitConfig @SpringJUnitConfig
@ -125,6 +126,22 @@ class DynamicPropertySourceNestedTests {
} }
} }
@Nested
class DynamicPropertySourceOverrideEnclosingClassTests {
@DynamicPropertySource
static void overrideDynamicPropertyFromEnclosingClass(DynamicPropertyRegistry registry) {
registry.add(TEST_CONTAINER_PORT, () -> -999);
}
@Test
@DisplayName("@Service has values injected from @DynamicPropertySource in enclosing class and nested class")
void serviceHasInjectedValues(@Autowired Service service) {
assertThat(service.getIp()).isEqualTo("127.0.0.1");
assertThat(service.getPort()).isEqualTo(-999);
}
}
static abstract class DynamicPropertySourceSuperclass { static abstract class DynamicPropertySourceSuperclass {

Loading…
Cancel
Save