Skip to content

Problems with TestContainers and Kotlin #318

Closed

Description

@iirekm

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.

Activity

  1. martin-g commented on Mar 31, 2017

    @martin-g

    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 ? :-)

  2. rnorth commented on Apr 1, 2017

    @rnorth
    Member

    Yeah, I'd not appreciated this would be a problem with Kotlin but it's unfortunate :(
    @bsideup, something to consider for the next version...

  3. asafm commented on Apr 1, 2017

    @asafm
    Contributor
  4. mpecan commented on Jul 18, 2017

    @mpecan

    This is related directly to #238 and can be resolved in the way that was suggested by @bsideup (#238 (comment)).

  5. mkobit commented on May 16, 2018

    @mkobit

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

    val container: GenericContainer<*> = GenericContainer<Nothing>()
  6. helmbold commented on May 29, 2018

    @helmbold

    @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.

  7. kiview commented on Oct 11, 2018

    @kiview
    Member

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

  8. alex0ptr commented on Oct 10, 2019

    @alex0ptr

    @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.

  9. xyzwps commented on Oct 29, 2019

    @xyzwps

    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")
  10. thaibt commented on Apr 23, 2021

    @thaibt

    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.

  11. schnapster commented on Oct 22, 2021

    @schnapster

    Looks like Kotlin 1.5.30 ships with a preview for 1.6 that fixes this: https://kotlinlang.org/docs/whatsnew1530.html#improvements-to-type-inference-for-recursive-generic-types

  12. sdeleuze commented on Dec 28, 2021

    @sdeleuze

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

  13. bsideup commented on Dec 28, 2021

    @bsideup
    Member

    @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!

  14. sdeleuze commented on Dec 28, 2021

    @sdeleuze

    Yep indeed that fixed a few Spring builder APIs as well. I am glad the Kotlin team has listened feedback from the community to fix this really annoying issue.

  15. sdeleuze commented on Mar 12, 2025

    @sdeleuze

    @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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions