Spring webflux + Flyway clean 禁用问题

问题描述 投票:0回答:1

我想使用 testcontainers 和 webflux 执行一些单元测试 因此,我创建了一个扩展来在每次启动测试时清理我的 postgres 数据库。

class ClearDatabaseExtension : BeforeAllCallback {
    @Throws(Exception::class)
    override fun beforeAll(extensionContext: ExtensionContext) {
        val flyway = SpringExtension.getApplicationContext(extensionContext)
            .getBean(Flyway::class.java)
        flyway.clean()
        flyway.migrate()
    }
}

这个行为是通过这个注解注入的:

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@ExtendWith(ClearDatabaseExtension::class)
@TestPropertySource(properties = ["flyway.cleanDisabled=false"])
annotation class ClearDatabase()

以这种方式应用于我的控制器:

@Testcontainers
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = ["flyway.cleanDisabled=false"]
)
@AutoConfigureWebTestClient
@ContextConfiguration(initializers = [Initializer::class])
@ClearDatabase
class XXXControllerTest(@Autowired val client: WebTestClient) {

    @Autowired
    lateinit var flyway: Flyway

    companion object {
        @Container
        val container: PostgreSQLContainer<Nothing> = PostgreSQLContainer<Nothing>("postgres:14-alpine")
            .apply {
                withDatabaseName("test")
                withUsername("test")
                withPassword("test")
            }

        @DynamicPropertySource
        @JvmStatic
        fun registerDynamicProperties(registry: DynamicPropertyRegistry) {
            registry.add("spring.r2dbc.url") { container.jdbcUrl.replace("jdbc:", "r2dbc:") }
            registry.add("spring.r2dbc.username") { container.username }
            registry.add("spring.r2dbc.password") { container.password }
        }
    }

}

这是我的 application.yml 文件中有关测试的摘录:

spring:
  config:
    activate:
      on-profile: test
  datasource:
    url: jdbc:tc:postgresql:14-alpine:///test
    username: test
    password: test
    jpa:
      hibernate:
        ddl-auto: create
  r2dbc:
    url: r2dbc:tc:postgresql:///testdb?TC_IMAGE_TAG=14-alpine
flyway:
  cleanDisabled: false

我的测试一直失败,并显示以下消息:

Unable to execute clean as it has been disabled with the 'flyway.cleanDisabled' property.
org.flywaydb.core.api.FlywayException: Unable to execute clean as it has been disabled with the 'flyway.cleanDisabled' property.

尽管我已在代码的多个部分中将此属性设置为 false,但我的测试总是因此错误而失败。任何想法 ? 致以诚挚的问候。

mocking spring-webflux flyway testcontainers
1个回答
0
投票

就我而言,在测试中将

@SpringBootTest
替换为
@SpringBootTest(properties = "spring.flyway.clean-disabled=false")
有助于解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.