其他分享
首页 > 其他分享> > 在SpringBoot项目里配置Flyway并借助TestContainer写集成测试

在SpringBoot项目里配置Flyway并借助TestContainer写集成测试

作者:互联网

配置flyway

    implementation 'org.flywaydb:flyway-core:8.0.1'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc:2.5.6'
	runtimeOnly 'org.postgresql:postgresql:42.3.0'

spring-boot-starter-jdbc可用于确保datasource可连接。

集成测试

当在项目中配置了数据库,又打算写集成测试时,有两种方案可以选择:

方案1

借助h2数据库搭建测试数据库

spring:
  datasource:
    url: jdbc:h2:~/test
    username: lead_store_user
    password: lead_store
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
class BooleanTest {
@Test
public void isTure {
assertThat(true, is(true));
}
}
@Configuration
public class TestConfiguration {
  @Bean
  @Profile("test")
  public FlywayMigrationStrategy cleanMigrateStrategy() {
    return Flyway::repair;
  }
}

方案2

借助TestContainer搭建测试数据库
参考文档:spring-boot-testcontainers-integration-test

    testImplementation 'org.testcontainers:postgresql:1.16.2'
    testImplementation 'org.testcontainers:testcontainers:1.15.3'
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = {IntegrationTestBase.DatabaseConnectionInjector.class})
public class IntegrationTestBase {
  private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER =
      new PostgreSQLContainer("postgres:12.8")
          .withDatabaseName("db_name")
          .withUsername("user_name")
          .withPassword("password");

  static {
    POSTGRE_SQL_CONTAINER.start();
  }

  static class DatabaseConnectionInjector
      implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
      TestPropertyValues.of(
              "spring.datasource.url=" + POSTGRE_SQL_CONTAINER.getJdbcUrl(),
              "spring.datasource.username=" + POSTGRE_SQL_CONTAINER.getUsername(),
              "spring.datasource.password=" + POSTGRE_SQL_CONTAINER.getPassword())
          .applyTo(applicationContext.getEnvironment());
    }
  }
}

在docker中运行测试时,如果出现如下报错java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration可以在TestContainer仓库的Issue中找到解决方案, 需要在测试容器中添加如下卷:

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

挂载的/var/run/docker.sock文件是Docker守护进程(Docker daemon)默认监听的Unix域套接字(Unix domain socket),容器中的进程可以通过它与Docker守护进程进行通信。

标签:TestContainer,SpringBoot,spring,Flyway,public,datasource,测试,org,class
来源: https://blog.csdn.net/weixin_45876795/article/details/121131281