我试图使用TestContainers中的PostgreSQLContainer(https://github.com/testcontainers/testcontainers-java+https://www.testcontainers.org/)来单元测试我的JPA存储库.

我这样声明我的容器:

private val postgresqlContainer = PostgreSQLContainer("postgres:12-alpine")

However, I encounter the following error, from Intellij IDE:

没有足够的信息来推断类型变量SELF

当我try 启动该服务时,完全错误是:

错误:(26,43)Kotlin:类型推断失败:信息不足 要推断构造函数PostgreSQLContainer<self中的参数self: PostgreSQLContainer<self!>!>(P0:字符串!)请具体说明 明确地说.

推荐答案

TestContainers depends on construction of generic type C<Self extends C<SELF>>, but Kotlin don't like that. My workaround was to define my own factory class:

class MyPostgreSQLContainer(imageName: String) : PostgreSQLContainer<MyPostgreSQLContainer>(imageName)

我可以这样使用它:

private val postgresqlContainer = MyPostgreSQLContainer("postgres:12-alpine")

Kotlin相关问答推荐

Compose:LaunchedEffect在密钥更改后不会重新启动

在Kotlin中求n个ClosedRange实例相交的最常用方法是什么?

KMP:未能添加cafe.adriel.voyager依赖项

Scala与Kotlin中的迭代

为什么 Kotlin 中没有 init 块的注释

如何在 Kotlin 中为类方法调用传递变量

在 kotlin 中使具体化字段可选

将 java Optional 转换为 Kotlin Arrow Option

Kotlin 协程按顺序执行,但仅在生产机器上执行

Kotlin 中的as Long和.toLong()有什么区别?

通用接口继承

Moshi:解析单个对象或对象列表(kotlin)

Kotlin 具体化的泛型不会按计划保持类型

在kotlin中,如何模拟封装回调函数?

在Kotlin中传递并使用函数作为构造函数参数

未找到导入 kotlinx.coroutines.flow.*

重复构建失败To use Coroutine features, you must add `ktx`......

以Kotlin为单位的货币数据类型

Kotlin - 如果不为空,则使用修改后的 Obj props 覆盖 Obj props

如何在 Fragment 中使用 Anko DSL?