testcontainers-java: Problems with TestContainers and Kotlin

TestContainers depends on construction of raw types and pattern like class C<SELF extends C<SELF>>. Unfortunately Kotlin, and probably other JVM languages don’t like it - see https://youtrack.jetbrains.com/issue/KT-17186

Adding static factory methods could solve those problems.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 58
  • Comments: 15 (4 by maintainers)

Most upvoted comments

My workaround is:

class KGenericContainer(imageName: String) : GenericContainer<KGenericContainer>(imageName)

And use it everywhere I’d need otherwise to use GenericContainer. Charming, isn’t it ? 😃

Another ugly way to handle this as described in the YouTrack issue:

val container: GenericContainer<*> = GenericContainer<Nothing>()

@helmbold Chaining is easily possible with Kotlin native scoping functions, example:

val container: GenericContainer<*> = GenericContainer<Nothing>()
                                        .apply{ withExposedPorts(6379) }

I’ll make a merge request to propose this in the Kotlin Examples.

First time I tried to use Testcontainers with Kotlin and ran into exactly this problem, so great it found the workarounds here 😅

Just reading this generic expression gives you a headache 😃 On Sat, 1 Apr 2017 at 20:09 Richard North notifications@github.com wrote:

Yeah, I’d not appreciated this would be a problem with Kotlin but it’s unfortunate 😦 @bsideup https://github.com/bsideup, something to consider for the next version…

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/testcontainers/testcontainers-java/issues/318#issuecomment-290933488, or mute the thread https://github.com/notifications/unsubscribe-auth/AA8Y8Rglpfbh-GydCPzdE7d_W1nkn8FMks5rroTOgaJpZM4Mvb7T .

I created a sub class of MySQLContainer:

internal class SpecifiedMySQLContainer(val image: String) : MySQLContainer<SpecifiedMySQLContainer>(image)

val mysqlContainer = SpecifiedMySQLContainer(image = "mysql:5.7.28").withDatabaseName("dbname")

@asomov I just tried val container: GenericContainer<*> = GenericContainer("someImage") with Kotlin 2.1.10 and it seems to work as expected. If you manage to reproduce, could you please share a repro?

Another ugly way to handle this as described in the YouTrack issue:

val container: GenericContainer<*> = GenericContainer<Nothing>()

This saved my life… Was to frustrated and I did not find this thread.

@mkobit Method chaining as shown in the docs is not possible with your approach ( GenericContainer<Nothing>()).

I suggest to use the workaround from @martin-g instead.

@sdeleuze good point! It is great to see Kotlin supporting more and more “in the wild” cases 🎉 IIRC some Spring’s builders also benefited from it? Double win!

Please close this issue as Kotlin 1.6 which fixes it has been released.