如何使用JUnit 5在Kotlin中创建TestContainers基础测试类 [英] How to create a TestContainers base test class in Kotlin with JUnit 5

查看:137
本文介绍了如何使用JUnit 5在Kotlin中创建TestContainers基础测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use Neo4j TestContainers with Kotlin, Spring Data Neo4j, Spring Boot and JUnit 5. I have a lot of tests that require to use the test container. Ideally, I would like to avoid copying the container definition and configuration in each test class.

目前我有类似的东西

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

如何提取databaseServer定义和@TestConfiguration?我尝试了不同的方法来创建基类并让ContainerTest对其进行扩展,但是它不起作用.据我了解,静态属性不是Kotlin继承的.

How can I extract the databaseServer definition and the @TestConfiguration? I tried different ways of creating a base class and having the ContainerTest extend it, but it is not working. From what I understand, static attriubutes are not inherited in Kotlin.

推荐答案

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

I've had the same issue (making Spring Boot + Kotlin + Testcontainers work together) and after searching the web for (quite) a while I found this nice solution: https://github.com/larmic/testcontainers-junit5. You'll just have to adopt it to your database.

这篇关于如何使用JUnit 5在Kotlin中创建TestContainers基础测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆