Better way to set testcontainer properties from default applicaton.yml in springboottest

流过昼夜 提交于 2021-01-29 14:16:04

问题


I am using posgresql testcontainer in springboottest. As I have multiple tests involving this testcontainer, hence I have used static testcontainer which will be invoked once for all tests of 1 junit class and shutdown after all tests are executed.

This I have implemented using ParameterResolver, BeforeEachCallback.

Problem with this approach is that datasource metadata like jdbc-url, db name , host , port configured in default application.yml is not used directly in testcontainer properties, instead I have hardcoded those values because springboot properties are not available at that time.

is there any better approach where I can use static testcontainers having BeforeEachCallback feature whose values are fetched from default application.yml ?

@SpringBootTest
class SampleTest extends TestContainerBase {

    @Test
    void test1() {
        //some code
    }

}

@ExtendWith(ContainerExtension.class)
@ResourceLock(Environment.ID)
public abstract class TestContainerBase {

    protected static String jdbcUrl;
    protected static String username;
    protected static String password;

    @BeforeAll
    static void prepareContainerEnvironment(Environment env) {
        jdbcUrl = env.getJdbcUrl();
        username = env.getUsername();
        password = env.getPassword();
    }

    @DynamicPropertySource
    static void dynamicPropertySource(DynamicPropertyRegistry registry) {

        registry.add("spring.datasource-.jdbc-url", () -> jdbcUrl);
        registry.add("spring.datasource-.username", () -> username);
        registry.add("spring.datasource-.password", () -> password);
        registry.add("spring.datasource-.driver-class-name", () -> "org.postgresql.Driver");
    }

}

public class ContainerExtension implements ParameterResolver, BeforeEachCallback {
    
    // overridden supportsParameter and resolveParameter
}

I want that myDB , sa , sa are read from application.yml. How can I get application.yml values here in this class ? As springboot context is not yet loaded so I am unable to think of any alternative to get those values.

public class ContainerResource extends Environment {

    @Container
    protected static PostgreSQLContainer postgreSQLContainer =
            new PostgreSQLContainer("artifactory.devtools.syd.c1.macquarie.com:9996/postgres:11")
                    .withDatabaseName("myDB")
                    .withUsername("username")
                    .withPassword("password");
    
    ContainerEnvironmentResource() {
        postgreSQLContainer.start();
        this.setJdbcUrl(postgreSQLContainer.getJdbcUrl());
        this.setUsername(postgreSQLContainer.getUsername());
        this.setPassword(postgreSQLContainer.getPassword());
    }
}

来源:https://stackoverflow.com/questions/63228109/better-way-to-set-testcontainer-properties-from-default-applicaton-yml-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!