为什么 TestContainer 使用错误的 URL缺少“tc”?

问题描述

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

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 中

我更喜欢使用构造函数自动装配,但我没有得到它,因为 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$2(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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...