为什么 TestContainer 使用错误的 URL(缺少 'tc')?

Why does TestContainer use a wrong URL (missing 'tc')?

我打算使用 Testcontainer(版本 1.15.2)和 Kotlin 1.4.31 测试数据库访问,我有一个测试 class...

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.reactive.server.WebTestClient
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers
import java.time.Instant
import java.util.*

//@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@Testcontainers
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//class PostgresTcTest(var dao: GenericJpaDao<Declaration>, var webTestClient: WebTestClient) {
class PostgresTcTest {
    @Autowired
    lateinit var webTestClient: WebTestClient
    @Autowired
    lateinit var dao: GenericJpaDao<Declaration>
//    val POSTGRES_IMAGE = DockerImageName.parse("postgres:13.2-alpine")
//
//    val container by lazy { PostgreSQLContainer<Nothing>(POSTGRES_IMAGE) }

    companion object {

        @Container
        val container = PostgreSQLContainer<Nothing>("postgres:13.2-alpine")/*.apply {
            withDatabaseName("testdb")
            withUsername("duke")
            withPassword("s3crEt")
        }*/

        @JvmStatic
        @DynamicPropertySource
        fun properties(registry: DynamicPropertyRegistry) {
            registry.add("spring.datasource.url", container::getJdbcUrl);
            registry.add("spring.datasource.password", container::getPassword);
            registry.add("spring.datasource.username", container::getUsername);
        }
    }
}

...但我总是遇到异常:

Driver org.testcontainers.jdbc.ContainerDatabaseDriver claims to not accept jdbcUrl, jdbc:postgresql://localhost:55017/test?loggerLevel=OFF

我有一个 application.yaml 包含:

spring:
  datasource:
    driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
    url: jdbc:tc:postgresql:13.2:///testdb?TC_INITSCRIPT=tc_initscript_postgresql.sql
    username: duke
    password: s3crEt
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQL95Dialect
    hibernate:
      ddl-auto: create-drop

我想知道为什么 TestContainers 徒劳地寻找 jdbc:postgresql 尽管我输入了 jdbc:tc :postgresql 在 application.yaml

我更喜欢使用构造函数 autowiring 但我没有得到它,因为 Kotlin 总是抱怨:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [de.xxx.dao.GenericJpaDao<de.xxx.model.Declaration> dao] in constructor [public de.xxx.db.PostgresTcTest(de.xxx.dao.GenericJpaDao<de.xxx.model.Declaration>,org.springframework.test.web.reactive.server.WebTestClient)].

    at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameter(ExecutableInvoker.java:200)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameters(ExecutableInvoker.java:183)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:74)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestClassConstructor(ClassBasedTestDescriptor.java:342)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateTestClass(ClassBasedTestDescriptor.java:289)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateTestClass(ClassTestDescriptor.java:79)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:267)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider(ClassBasedTestDescriptor.java:259)

我无法通过构造函数自动装配或在字段中使用@Autowired 来获得 GenericJpaDaoWebTestClient 的实例。


根据WebTestClient回答: 这可以通过使用 @AutoConfigureWebTestClient

来实现

不需要设置driver-class-name

将其从 application.properties

中删除
spring:
  datasource:
    url: jdbc:tc:postgresql:13.2:///testdb?TC_INITSCRIPT=tc_initscript_postgresql.sql
    username: duke
    password: s3crEt
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQL95Dialect
    hibernate:
      ddl-auto: create-drop