How to inherit @DynamicPropertySource in @RegisterExtension?

Viewed 101

I want to create a junit rule that can be loaded with @RegisterExtension. In it, I want a @DynamicPropertySource to run by spring, so that every class that uses this extension will get an additional application property injected during tests:

public class MyMockServer implements BeforeEachCallback, AfterEachCallback {
    @DynamicPropertySource
    static void randomPortInitializer(DynamicPropertyRegistry registry) {
        registry.add("app.test.url", () -> "http://localhost:8080"); //lateron random port
    }
}
    
    
class JunitTest { 
    @RegisterExtension
    public static final MyMockServer mock = new MyMockServer();
    
    @Test
    public void test(@Value("${app.test.url}" String url) {
        assertEquals("http://localhost:8080", url);
    }
}

Result:

nested exception is java.lang.IllegalArgumentException:
Could not resolve placeholder 'app.test.url' in value "${app.test.url}"
0 Answers
Related