【问题标题】:TestContainers PostgreSQLContainer with Kotlin unit test: "Not enough information to infer type variable SELF"TestContainers PostgreSQLContainer 与 Kotlin 单元测试:“没有足够的信息来推断类型变量 SELF”
【发布时间】:2020-03-19 07:16:59
【问题描述】:

我正在尝试使用 TestContainers 中的PostgreSQLContainer (https://github.com/testcontainers/testcontainers-java + https://www.testcontainers.org/) 来对我的 JPA 存储库进行单元测试。

我这样声明我的容器:

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

但是,我遇到了以下错误,来自 Intellij IDE

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

我尝试启动服务时的完整错误是:

错误:(26, 43) Kotlin:类型推断失败:信息不足 在构造函数 PostgreSQLContainer!>(p0: String!) 请指定 明确的。

【问题讨论】:

    标签: java spring-boot kotlin testcontainers


    【解决方案1】:

    这个技巧也有效

    private val postgresqlContainer = PostgreSQLContainer<Nothing>().apply {
        withDatabaseName("x")
        withUsername("y")
        withPassword("z")
    }
    

    【讨论】:

      【解决方案2】:

      TestContainers 依赖于泛型类型 C&lt;Self extends C&lt;SELF&gt;&gt; 的构造,但 Kotlin 不喜欢这样。 我的解决方法是定义我自己的工厂类:

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

      我可以这样使用它:

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

      【讨论】:

        【解决方案3】:

        此问题现已在 Kotlin 方面得到修复,请参阅 this blog post 了解更多详细信息。需要 Kotlin 1.5.30 的标志,这将是 Kotlin 1.6.0 的默认标志。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-29
          • 1970-01-01
          • 2021-02-11
          • 2020-03-31
          • 1970-01-01
          • 2020-03-11
          相关资源
          最近更新 更多