【问题标题】:Testcontainers start two containers instead of one in Spring boot project测试容器在 Spring Boot 项目中启动两个容器而不是一个
【发布时间】:2021-09-05 04:27:02
【问题描述】:

我正在使用带有 Spring Boot 2.4 和 Junit5 的 Testcontainers 1.15.3。 当我运行测试时,testcontainers 会启动第一个容器并执行 flyway 脚本,然后停止第一个容器。立即启动第二个容器(不启动 flyway 脚本)。 我的测试失败了,因为第二个容器不包含数据。

抽象类:

@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@TestPropertySource(locations = "classpath:application-test.properties")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public abstract class AbstractIntegrationTest {
//...
}

测试类:

class ClassTest extends AbstractIntegrationTest{
    
    @Test
    void getById () throws Exception {
    //...
    }

}

用于测试的属性文件(jdbc url 包含 jdbc:tc 以启动 testcontainer):

spring.flyway.locations = classpath:database/structure,classpath:database/data
spring.datasource.url=jdbc:tc:postgresql:13.3:///databasename?TC_INITSCRIPT=file:src/test/resources/database/dataset/add_user.sql

启动测试后的日志:

...
...
2021-06-21 12:56:52 [main] INFO  ???? [postgres:13.3] - Creating container for image: postgres:13.3
2021-06-21 12:56:52 [main] INFO  ???? [postgres:13.3] - Starting container with ID: 6a41054e8ec0f9045f8db9e945134234458a0e60b6157618f6f139cdf77d0cc4
2021-06-21 12:56:52 [main] INFO  ???? [postgres:13.3] - Container postgres:13.3 is starting: 6a41054e8ec0f9045f8db9e945134234458a0e60b6157618f6f139cdf77d0cc4
...
...
2021-06-21 12:56:53 [main] INFO  o.f.core.internal.command.DbMigrate - Migrating schema "public" to version "1.1.001 - init structure"
...
...
2021-06-21 12:56:55 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
2021-06-21 12:56:55 [main] INFO  ???? [postgres:13.3] - Creating container for image: postgres:13.3
2021-06-21 12:56:55 [main] INFO  ???? [postgres:13.3] - Starting container with ID: f02fccb0706f047918d849f897ce52bf41870a53821663b21212760c779db05f
2021-06-21 12:56:55 [main] INFO  ???? [postgres:13.3] - Container postgres:13.3 is starting: f02fccb0706f047918d849f897ce52bf41870a53821663b21212760c779db05f

正如我们在上面的日志中看到的,创建了两个容器。

你能帮我解决这个问题吗?

谢谢。

【问题讨论】:

  • 也许我遗漏了一些东西,但我无法在您的代码中找到首先使用 testcontainers lib 的地方。你能澄清一下吗?
  • testcontainers 被自动使用,因为在上面的属性文件中 jdbc url 以 jdbc:tc 开头(tc for testcontainers)
  • 我怀疑是弹簧初始化问题导致加载两次,或者可能是 tescontainers 的问题
  • Spring Boot 2.5.3 和 liquibase、TestContainer 1.16、具有多个数据源的 Junit 5 也有同样的问题
  • @François-DavidLessard 请参阅下面的回复,希望对您有所帮助

标签: java spring-boot junit5 flyway testcontainers


【解决方案1】:

我找到了适合我的案例的解决方案:删除 flyway 用户和密码属性以仅使用 spring 属性。这些属性的重复导致数据源的双重启动。

之前

spring:
  flyway:
    locations: [ classpath:flyway-scripts ]
    user: xxx
    password: xxx
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: xxx
    password: xxx

之后

spring:
  flyway:
    locations: [ classpath:flyway-scripts ]
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: xxx
    password: xxx

【讨论】:

    【解决方案2】:

    我修复它的方法是将?TC_DAEMON=true 添加到数据源url。 (就我而言,我使用了postgis,所以只需将其替换为jdbc:tc:postgresql:13.3

    spring:
      datasource:
        driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
        url: jdbc:tc:postgis:9.6-2.5:///dbname?TC_DAEMON=true
        username: xxx
        password: xxx
      flyway:
        enabled: true
        locations: 'classpath:db/migration'
        url: ${spring.datasource.url}
        user: ${spring.datasource.username}
        password: ${spring.datasource.password}
        validate-on-migrate: true
    

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2020-05-10
      • 2019-09-16
      • 2015-04-15
      • 2020-06-20
      • 2022-01-19
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      相关资源
      最近更新 更多