Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Nested classes with nested @DynamicPropertySource get called in order, but don't get applied #37040

Closed
krodyrobi opened this issue Aug 21, 2023 · 2 comments
Labels
for: external-project For an external project and not something we can fix status: superseded An issue that has been superseded by another

Comments

@krodyrobi
Copy link

It seems that @Nested and @DynamicPropertySource do not play together when both outer and inner classes have a source defined. Neither @DirtiesContext nor inheriting / overwritting seems to do the trick, (overwritting expects a full setup, while inheriting has the methods called in proper order but they don't seem to get applied).

I've added a manual test to the existing test class that should fail as I'd expect "1" to be the host but it is not.

SpringJUnitConfig
class DynamicPropertySourceNestedTests {

    private static final String TEST_CONTAINER_IP = "DynamicPropertySourceNestedTests.test.container.ip";

    private static final String TEST_CONTAINER_PORT = "DynamicPropertySourceNestedTests.test.container.port";

    static DemoContainer container = new DemoContainer();

    @DynamicPropertySource
    static void containerProperties(DynamicPropertyRegistry registry) {
        registry.add(TEST_CONTAINER_IP, container::getIpAddress);
        registry.add(TEST_CONTAINER_PORT, container::getPort);
    }

    private static void assertServiceHasInjectedValues(Service service) {
        assertThat(service.getIp()).isEqualTo("127.0.0.1");
        assertThat(service.getPort()).isEqualTo(4242);
    }

    @Test
    @DisplayName("@Service has values injected from @DynamicPropertySource")
    void serviceHasInjectedValues(@Autowired Service service) {
        assertServiceHasInjectedValues(service);
    }

    @Nested
    class Testing {
        @DynamicPropertySource
        static void containerProperties(DynamicPropertyRegistry registry) {
            registry.add(TEST_CONTAINER_IP, () -> "1"); // even though it is called after outer dynamic properties it does not get applied
            registry.add(TEST_CONTAINER_PORT, container::getPort);
        }

        @Test
        void serviceHasInjectedValues(@Autowired Service service) {
            assertServiceHasInjectedValues(service);
        }
    }

    @Configuration
    @Import(Service.class)
    static class Config {
    }

    static class Service {

        private final String ip;

        private final int port;


        Service(@Value("${" + TEST_CONTAINER_IP + ":10.0.0.1}") String ip, @Value("${" + TEST_CONTAINER_PORT + ":-999}") int port) {
            this.ip = ip;
            this.port = port;
        }

        String getIp() {
            return this.ip;
        }

        int getPort() {
            return this.port;
        }
    }

    static class DemoContainer {

        String getIpAddress() {
            return "127.0.0.1";
        }

        int getPort() {
            return 4242;
        }
    }
}
@quaff
Copy link
Contributor

quaff commented Aug 21, 2023

It's belong to spring-framework not spring-boot, I've created spring-projects/spring-framework#31083 to fix it.

@bclozel
Copy link
Member

bclozel commented Aug 21, 2023

Superseded by spring-projects/spring-framework#31083

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Aug 21, 2023
@bclozel bclozel added for: external-project For an external project and not something we can fix status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: superseded An issue that has been superseded by another
Projects
None yet
4 participants