Browse Source

Revise contribution

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.

However, since Spring Framework 5.3.x is built using Java 8, this
commit removes DynamicPropertySourceOverridesEnclosingClassTests since
it declares a static method in a @Nested (inner) test class, which
results in a compiler error on Java 8.

See https://bugs.openjdk.org/browse/JDK-8254321
See gh-31085
pull/31598/head
Sam Brannen 2 years ago
parent
commit
d7ac89ecc9
  1. 7
      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

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

@ -1,5 +1,5 @@ @@ -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 @@ -55,6 +55,11 @@ class DynamicPropertiesContextCustomizerFactory implements ContextCustomizerFact
}
private void findMethods(Class<?> testClass, Set<Method> 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);
}

17
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 @@ -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 { @@ -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 {

Loading…
Cancel
Save