栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

配置flyway
  • 配置application.yml文件
    可以在Spring Boot Reference documentation的Application Properties中找到关于application.yml的各种配置
    • 配置数据源
      spring:
        profiles:
          active: ${SPRING_PROFILE}
        datasource:
          url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
          username: ${DB_USER}
          password: ${DB_USER_PASSWORD}
          driver-class-name: org.postgresql.Driver
      
    • 开启详细日志打印(在定位问题时很有用):
      logging:
        level:
          root: debug
      
  • 在build.gradle里添加依赖
    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数据库搭建测试数据库

  • 在build.gradle文件中添加依赖:testImplementation ‘com.h2database:h2:1.4.200’
  • 在application-test.yml中添加配置:
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));
}
}
  • 在配置了flyway的情况下,若执行flyway管理的sql报错之后,修复sql正确后,可以在测试中加上如下Bean
@Configuration
public class TestConfiguration {
  @Bean
  @Profile("test")
  public FlywayMigrationStrategy cleanMigrateStrategy() {
    return Flyway::repair;
  }
}
方案2

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

  • 在build.gradle文件中添加依赖:
    testImplementation 'org.testcontainers:postgresql:1.16.2'
    testImplementation 'org.testcontainers:testcontainers:1.15.3'
  • 编写继承测试的base类,并由其他集成测试类继承
@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 {

    @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守护进程进行通信。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425293.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号