From d7ac89ecc99174b8a7ec174f01bd7025e62624e3 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 21 Aug 2023 14:17:05 +0200 Subject: [PATCH] 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 --- ...namicPropertiesContextCustomizerFactory.java | 7 ++++++- .../DynamicPropertySourceNestedTests.java | 17 ----------------- 2 files changed, 6 insertions(+), 18 deletions(-) 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 b0e636dc7e1..7f9e3da7798 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 06412e7eec8..08113cee82c 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 {