问题描述
我正在尝试将可重启的测试容器集成到使用 JUnit 和 Kotlin 的应用程序中。
我目前的做法是在我设置容器和 Spring Boot 属性的每个测试套件中都有一个配套类,如下所示:
companion object {
@Container
private val postgres = PostgreSQLContainer<Nothing>("postgres:12.3").apply {
withDatabaseName("xxx")
withExposedPorts(5432)
withUsername("xxx")
}
@DynamicPropertySource
@JvmStatic
fun registerProperties(registry: DynamicPropertyRegistry) {
registry.add("spring.datasource.url",container::getJdbcUrl)
registry.add("spring.datasource.username",container::getUsername)
registry.add("spring.datasource.password",container::getPassword)
}
}
但由于它位于编译为静态块的块中,因此容器不会在每次测试时重新启动(根据我观察到的行为和 the official docs)。
当我尝试将容器放在伴随对象块之外时,Spring 无法正确设置动态道具并抛出 java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
。
尝试使用 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
达到我的结果对行为没有影响。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)