TestContainers PostgreSQLContainer with Kotlin unit test: "Not enough information to infer type variable SELF"
Asked Answered
Q

3

10

I am trying to use PostgreSQLContainer from TestContainers (https://github.com/testcontainers/testcontainers-java + https://www.testcontainers.org/) in order to unit test my JPA repositories.

I declare my container like that:

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

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

Not enough information to infer type variable SELF

The full error when I try to launch the service is:

Error:(26, 43) Kotlin: Type inference failed: Not enough information to infer parameter SELF in constructor PostgreSQLContainer<SELF : PostgreSQLContainer<SELF!>!>(p0: String!) Please specify it explicitly.

Quasijudicial answered 23/11, 2019 at 12:5 Comment(0)
Q
11

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)

And I can use it like that:

private val postgresqlContainer = MyPostgreSQLContainer("postgres:12-alpine")
Quasijudicial answered 26/11, 2019 at 7:24 Comment(0)
R
25

This trick also works

private val postgresqlContainer = PostgreSQLContainer<Nothing>().apply {
    withDatabaseName("x")
    withUsername("y")
    withPassword("z")
}
Replenish answered 20/2, 2020 at 20:32 Comment(0)
Q
11

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)

And I can use it like that:

private val postgresqlContainer = MyPostgreSQLContainer("postgres:12-alpine")
Quasijudicial answered 26/11, 2019 at 7:24 Comment(0)
I
6

This is now fixed on Kotlin side, see this blog post for more details. Require a flag with Kotlin 1.5.30 and will be the default as of Kotlin 1.6.0.

Isotone answered 8/9, 2021 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.