我正在尝试:
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
)
@EnableAutoConfiguration(
exclude = [
ReactiveOAuth2ClientAutoConfiguration::class,
ReactiveOAuth2ResourceServerAutoConfiguration::class,
]
)
@ActiveProfiles("testcontainers")
internal class RecordIntegrationTest {
@Test
fun contextLoads() {}
}
与 testcontainers
application.yml中的配置文件:
spring:
config.activate.on-profile: testcontainers
r2dbc.url: r2dbc:tc:postgresql:///databasename?TC_IMAGE_TAG=13.2
flyway:
enabled: false
但是它尝试两次创建存储库(可能还有其他bean):
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'recordRepository' defined in com.example.RecordRepository defined in @EnableR2dbcRepositories declared on R2dbcRepositoriesAutoConfigureRegistrar.EnableR2dbcRepositoriesConfiguration: Cannot register bean definition [Root bean: class [org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in com.example.RecordRepository defined in @EnableR2dbcRepositories declared on R2dbcRepositoriesAutoConfigureRegistrar.EnableR2dbcRepositoriesConfiguration] for bean 'recordRepository': There is already [Root bean: class [org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in com.example.RecordRepository defined in @EnableR2dbcRepositories declared on R2dbcRepositoriesAutoConfigureRegistrar.EnableR2dbcRepositoriesConfiguration] bound.
更新
当我将测试移到根包时发现它运行良好
src/test/kotlin/com/example/backend
但是当我把它放进包裹里的时候它就失败了
`src/test/kotlin/com/example/backend/subfolder`
更新2
我发现如果 src/main/kotlin
与出现异常的集成测试在同一个包中。
例如。:
data class X(@Id id: Long? = null)
// src/main/kotlin/com/example/x/XRepository.kt
interface XRepository : CoroutineCrudRepository<X, Long> {}
此测试失败,出现上述异常:
// src/test/kotlin/com/example/x/XIntegrationTest.kt
@SpringBootTest(classes = [BackendApplication::class])
@EnableAutoConfiguration(
exclude = [
ReactiveOAuth2ClientAutoConfiguration::class,
ReactiveOAuth2ResourceServerAutoConfiguration::class,
]
)
@ActiveProfiles("testcontainers")
internal class XIntegrationTest {
@Test
fun contextLoads() {
}
}
删除存储库或将集成测试移到另一个包可以正常工作。
2条答案
按热度按时间jgwigjjp1#
在所使用的排除块中包含datasourceautoconfiguration
EnableR2dbcRepositories
在启动集成测试时禁用对数据源的检查Repositories
或者您可以配置datasource
来解决这个问题。问题主要是由于存储库所需的数据源不可用
hm2xizp92#
在你生命中的某个地方
src/main/java
你有一个类带来了注解@EnableAutoConfiguration
. 这是在定义了自己的类之后,要从测试中排除的类@EnableAutoConfiguration
在测试包的其他类中。