TestContainers共享数据库

问题描述

我正在尝试在JUnit 5中使用一个PostgreSQL TestContainer,以扩展我所有的测试类。目前,只有一个测试类通过此TestContainer通过/测试,其余的给我一个“无法获取JDBC连接”错误。

这是我的父类的样子:

@Testcontainers
@ContextConfiguration(initializers = SchemaMigrationApplicationTests.Initializer.class)
public class SchemaMigrationApplicationTests {

    public static final String BC_BASE_DOMAIN = "proxy-si.broadcloudpbx.net";
    public static final String EDOS_PROFILE = "asdf1234";
    public static final Region COUNTRY_REGION = Region.INT;

    private static final String DATABASE_NAME = "postgres";
    private static final String DATABASE_PASSWORD = "postgres";
    private static final String DATABASE_USERNAME = "username";

    @Container
    public static PostgreSQLContainer postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer()
            .withDatabaseName(DATABASE_NAME)
            .withPassword(DATABASE_PASSWORD)
            .withUsername(DATABASE_USERNAME)
            .withReuse(true);

    static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
                TestPropertyValues.of(
                    "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),"spring.datasource.username=" + postgreSQLContainer.getUsername(),"spring.datasource.password=" + postgreSQLContainer.getPassword()).applyTo(configurableApplicationContext.getEnvironment());
            
        }
    }
}

我只是让我的测试类扩展了SchemaMigrationApplicationTests。我还尝试过使用静态方法,并在此父类的容器上调用.start(),但这没有用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...