我打算使用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 来GenericJpaDao
获取实例。WebTestClient
根据 WebTestClient 回答:这可以通过使用来实现@AutoConfigureWebTestClient