pix*_*xel 4 spring kotlin spring-boot spring-framework-beans
我在生产文件中有以下配置:
@Configuration
internal class Config {
@Bean
fun clock() = Clock.systemUTC()
}
Run Code Online (Sandbox Code Playgroud)
在测试中:
@Configuration
class ClockTestConfiguration {
@Bean
fun clock() = SetableClock()
}
Run Code Online (Sandbox Code Playgroud)
我的测试注释:
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [
MyApplication::class,
ClockTestConfiguration::class
]
)
class MyTest {
...
Run Code Online (Sandbox Code Playgroud)
当我使用Spring Boot时,2.0.5.RELEASE它就像一个魅力.2.1.0.RELEASE在bean注册期间升级到它失败.
Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'clock' defined in com.foo.clock.ClockTestConfiguration:
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=clockTestConfiguration; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred);
defined in com.foo.clock.ClockTestConfiguration] for bean 'clock': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=config; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foo/clock/Config.class]] bound.
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:894)
Run Code Online (Sandbox Code Playgroud)
是否有一种干净的方式来覆盖这样的bean?
And*_*son 11
您可以使用要设置的properties属性.@SpringBootTestspring.main.allow-bean-definition-overriding=true
| 归档时间: |
|
| 查看次数: |
2001 次 |
| 最近记录: |