testcontainers共享数据库

ffdz8vbo  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(193)

我试图在junit5中有一个postgresqltestcontainer,我的所有测试类都扩展它。目前,我的测试类中只有一个通过/通过了使用这个testcontainer的测试,其余的给我一个“unable to acquire jdbc connection”错误。
我的父类是这样的:

@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(),但没有成功。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题