微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。