如何使用 JUnit 5 在 Kotlin 中创建 TestContainers 基测试类

Mar*_* L. 7 kotlin spring-data-neo4j testcontainers

我正在尝试将 Neo4j TestContainers 与 Kotlin、Spring Data Neo4j、Spring Boot 和 JUnit 5 一起使用。我有很多测试需要使用测试容器。理想情况下,我希望避免在每个测试类中复制容器定义和配置。

目前我有类似的东西:

@Testcontainers
@DataNeo4jTest
@Import(Neo4jConfiguration::class, Neo4jTestConfiguration::class)
class ContainerTest(@Autowired private val repository: XYZRepository) {

    companion object {
        const val IMAGE_NAME = "neo4j"
        const val TAG_NAME = "3.5.5"

        @Container
        @JvmStatic
        val databaseServer: KtNeo4jContainer = KtNeo4jContainer("$IMAGE_NAME:$TAG_NAME")
                .withoutAuthentication()
    }

    @TestConfiguration
    internal class Config {
        @Bean
        fun configuration(): Configuration = Configuration.Builder()
                .uri(databaseServer.getBoltUrl())
                .build()
    }

    @Test
    @DisplayName("Create xyz")
    fun testCreateXYZ() {
        // ...
    }

}

class KtNeo4jContainer(val imageName: String) : Neo4jContainer<KtNeo4jContainer>(imageName)
Run Code Online (Sandbox Code Playgroud)

如何提取数据库服务器定义和@TestConfiguration?我尝试了不同的方法来创建基类并让 ContainerTest 扩展它,但它不起作用。据我了解,静态属性在 Kotlin 中不是继承的。

Dan*_*has 1

我遇到了同样的问题(使 Spring Boot + Kotlin + Testcontainers 一起工作),在网上搜索(相当)一段时间后,我发现了这个很好的解决方案: https: //github.com/larmic/testcontainers-junit5。您只需将其采用到您的数据库中即可。