如何解决使用 Kotlin、JUnit 和 Spring Boot 使测试容器可重新启动
我正在尝试将可重启的测试容器集成到使用 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 举报,一经查实,本站将立刻删除。