Kotlin与Spring Boot的集成测试

Kotlin与Spring Boot的集成测试,spring,spring-boot,kotlin,integration-testing,testcontainers,Spring,Spring Boot,Kotlin,Integration Testing,Testcontainers,我拥有的:我正在开发一个微服务,使用SpringBoot和Web以及MongoDB作为存储。对于CI集成测试,我使用测试容器。我有两个带有SpringBootTest注释的集成测试,它们使用TestConfig类。TestConfig类为setup MongoDB测试容器提供了固定的公开端口 我的问题是:当我一次运行一个测试时,它们就会成功。但当我同时运行测试时,它们失败了 MongoContainerConfig.kt @TestConfiguration class MongoContain

我拥有的:我正在开发一个微服务,使用SpringBoot和Web以及MongoDB作为存储。对于CI集成测试,我使用测试容器。我有两个带有SpringBootTest注释的集成测试,它们使用TestConfig类。TestConfig类为setup MongoDB测试容器提供了固定的公开端口

我的问题是:当我一次运行一个测试时,它们就会成功。但当我同时运行测试时,它们失败了

MongoContainerConfig.kt

@TestConfiguration
class MongoContainerConfig {

    var mongoContainer: GenericContainer<Nothing>

    constructor() {
        mongoContainer = FixedHostPortGenericContainer<Nothing>("mongo")
                .withFixedExposedPort(27018,27017)
        mongoContainer.start()
    }

    @PreDestroy
    fun close() {
        mongoContainer.stop()
    }
}
@SpringBootTest(classes = arrayOf(MongoContainerConfig::class, AssertUtilsConfig::class))
class PositiveTest {

    @Test
    fun test() {}
}
第二次测试

@TestConfiguration
class MongoContainerConfig {

    var mongoContainer: GenericContainer<Nothing>

    constructor() {
        mongoContainer = FixedHostPortGenericContainer<Nothing>("mongo")
                .withFixedExposedPort(27018,27017)
        mongoContainer.start()
    }

    @PreDestroy
    fun close() {
        mongoContainer.stop()
    }
}
@SpringBootTest(classes = arrayOf(MongoContainerConfig::class, AssertUtilsConfig::class))
class PositiveTest {

    @Test
    fun test() {}
}
错误消息

@SpringBootTest(
    classes = arrayOf(MongoContainerConfig::class, AssertUtilsConfig::class),
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
class CardControllerTest {

    @Test
    fun test() {}
}
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoContainerConfig': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.lang.card.engcard.config.MongoContainerConfig$$EnhancerBySpringCGLIB$$e58ffeee]: Constructor threw exception; nested exception is org.testcontainers.containers.ContainerLaunchException: Container startup failed
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1320)
    a
您可以通过CI在github上看到此项目

这很有趣,因为如果将测试重写为java,测试就会工作(哈,这不容易) 测试具有不同的上下文以及MongoContainerConfig调用两次的原因。 修复-在CardControllerTest.kt中添加webEnv as

@SpringBootTest(classes = arrayOf(MongoContainerConfig::class, AssertUtilsConfig::class),
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class PositiveTest 
证明(哈,这不容易) 测试具有不同的上下文以及MongoContainerConfig调用两次的原因。 修复-在CardControllerTest.kt中添加webEnv as

@SpringBootTest(classes = arrayOf(MongoContainerConfig::class, AssertUtilsConfig::class),
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class PositiveTest 
证明