简体   繁体   English

Kotlin 集成测试与 Spring 引导

[英]Kotlin integration Tests with Spring Boot

What I have: I'm developing a microservice, using Spring Boot with Web and MongoDB as storage.我所拥有的:我正在开发一个微服务,使用 Spring Boot 和 Web 和 MongoDB 作为存储。 For CI integration tests I use test containers.对于 CI 集成测试,我使用测试容器。 I have two integrations tests with SpringBootTest annotations, which use TestConfig class.我有两个带有 SpringBootTest 注释的集成测试,它们使用 TestConfig class。 TestConfig class provides setup MongoDB test container with fixed exposed ports. TestConfig class 提供带有固定暴露端口的设置 MongoDB 测试容器。

My problem: When I run my tests one at a time then they succeed.我的问题:当我一次运行一个测试时,它们会成功。 But when I run my tests at the same time then they failed.但是当我同时运行我的测试时,它们失败了。

MongoContainerConfig.kt MongoContainerConfig.kt

@TestConfiguration
class MongoContainerConfig {

    var mongoContainer: GenericContainer<Nothing>

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

    @PreDestroy
    fun close() {
        mongoContainer.stop()
    }
}

First test第一次测试

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

    @Test
    fun test() {}
}

Second test第二次测试

@SpringBootTest(classes = arrayOf(MongoContainerConfig::class, AssertUtilsConfig::class))
class PositiveTest {

    @Test
    fun test() {}
}

Error msg错误信息

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

This project you can see on github with CI https://github.com/GSkoba/eng-card/runs/576320155?check_suite_focus=true这个项目你可以在 github 上看到 CI https://github.com/GSkoba/eng-card/runs/576320155?check_suite_focus=true

It's very funny because tests work if rewrite them to java这很有趣,因为如果将测试重写为 java

Hah, it not easy) https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/testing.html#testcontext-ctx-management-caching Tests have different context and its why MongoContainerConfig call twice.哈,这不容易) https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/testing.html#testcontext-ctx-management-caching测试有不同的上下文及其原因MongoContainerConfig 调用两次。 Fix - add webEnv as in CardControllerTest.kt修复 - 在 CardControllerTest.kt 中添加 webEnv

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

Proof https://github.com/GSkoba/eng-card/actions/runs/78094215证明https://github.com/GSkoba/eng-card/actions/runs/78094215

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM