具有Kotlin,TestContainers和外部配置的SpringBootTest

问题描述

我在为测试类创建外部配置时遇到问题。现在,我与Redis相关的测试必须在其中包含一个伴随对象

@Testcontainers
@TestPropertySource("classpath:/application-test.properties")
@SpringBootTest
class RedisRelatedTest {

  companion object {
    @Container
    val container = GenericContainer<Nothing>("redis:5.0.7-alpine")
            .apply { withExposedPorts(6379) }

    @JvmStatic
    @DynamicPropertySource
    fun properties(registry: DynamicPropertyRegistry) {
        registry.add("spring.redis.host",container::getHost);
        registry.add("spring.redis.port",container::getFirstMappedPort);
    }
  }
  ... some tesitng
}

我想将其移动到外面的某个地方,并使用一个衬板将其包括在内,但是我找不到一种可行的方法。我已经创建了带有其伴随对象的RedisConfig类,但是@Import(RedisConfig :: class)或@Import(RedisConfig.Congig :: class)被完全忽略了。

解决方法

@Import(RedisConfig :: class)本身不执行任何操作,您仍然必须自动装配一个bean,您是否也这样做?

另一种选择是拥有一个带有容器的抽象类,并将其扩展到测试类中。

,

@Import(RedisConfig::class) 不起作用。 如果是@SpringTest,您应该使用:

googleCallback

但我不确定 testcontainer 注释是否会按您的预期工作。我按照我在对类似问题的回应中描述的方式解决了这个问题:https://stackoverflow.com/a/66532851/3484423

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...