diff --git a/spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java b/spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java index b0e636dc7e..7f9e3da779 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 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. @@ -55,6 +55,11 @@ class DynamicPropertiesContextCustomizerFactory implements ContextCustomizerFact } private void findMethods(Class testClass, Set methods) { + // Beginning with Java 16, inner classes may contain static members. + // We therefore need to search for @DynamicPropertySource methods in the + // current class after searching enclosing classes so that a local + // @DynamicPropertySource method can override properties registered in + // an enclosing class. if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) { findMethods(testClass.getEnclosingClass(), methods); } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java index 06412e7eec..08113cee82 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java @@ -39,7 +39,6 @@ import static org.springframework.test.context.NestedTestConfiguration.Enclosing * {@link SpringExtension} in a JUnit Jupiter environment. * * @author Sam Brannen - * @author Yanming Zhou * @since 5.3.2 */ @SpringJUnitConfig @@ -126,22 +125,6 @@ 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 {