Что у меня есть: есть микросервис на Spring boot, с вебом и MongoDB как сторадж. Для интеграционных тестов я использую test container. Для тестирования микросервиса написаны 2 интаграционных теста с SpringBootTest аннатацией и для них есть TestConfig класс для поднятия контейнера 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()
}
}
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
Данный проект есть на github и данную ошибку можно посмотреть на CI https://github.com/GSkoba/eng-card/runs/576320155?check_suite_focus=true
Самое смешное, что если переписать тесты и конфиг на java, то все работает)