在 Spring 引导应用程序中启用测试配置并覆盖配置
Enabling test configuration and overriding config in Spring Boot application
目前我在一个与 PubSub 交互的项目中工作,但我希望它也能与 TestSupportBinderAutoConfiguration
一起工作(执行测试而不去 Google 检索凭据)。
spring.profiles.active= pubsub
spring.main.web-application-type=none
spring.cloud.stream.default-binder=test
spring.cloud.stream.bindings.output.destination= output-queue
spring.cloud.stream.bindings.input.destination= input-queue
默认情况下,所有与Google相关的AutoConfiguration
类都包含在@SpringBootApplication
[=18]的exclude
属性中=]
但是我注意到如果父 application.properties
属性有一些我想覆盖的属性,应用程序 类 仍然用那些而不是被覆盖的属性实例化,这意味着我正在尝试将 TestSupportBinder 类 与 PubSub 一起使用。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyProcessor.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource(properties = {
"--spring.profiles.active="
})
有什么解决方法吗?这样 main 类 就配置了新覆盖的属性,而不是从测试和 main 的不同地方收集配置?
实际上,您是在描述集成(使用 google)与单元测试(不使用 google)。通常我们将两者分开。例如,我们有一个单独的集成模块 https://github.com/spring-cloud/spring-cloud-stream/tree/master/spring-cloud-stream-integration-tests 并且 Rabbit 和 Kafka 本身也位于单独的模块中以进行自己的集成测试。
目前我在一个与 PubSub 交互的项目中工作,但我希望它也能与 TestSupportBinderAutoConfiguration
一起工作(执行测试而不去 Google 检索凭据)。
spring.profiles.active= pubsub
spring.main.web-application-type=none
spring.cloud.stream.default-binder=test
spring.cloud.stream.bindings.output.destination= output-queue
spring.cloud.stream.bindings.input.destination= input-queue
默认情况下,所有与Google相关的AutoConfiguration
类都包含在@SpringBootApplication
[=18]的exclude
属性中=]
但是我注意到如果父 application.properties
属性有一些我想覆盖的属性,应用程序 类 仍然用那些而不是被覆盖的属性实例化,这意味着我正在尝试将 TestSupportBinder 类 与 PubSub 一起使用。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyProcessor.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource(properties = {
"--spring.profiles.active="
})
有什么解决方法吗?这样 main 类 就配置了新覆盖的属性,而不是从测试和 main 的不同地方收集配置?
实际上,您是在描述集成(使用 google)与单元测试(不使用 google)。通常我们将两者分开。例如,我们有一个单独的集成模块 https://github.com/spring-cloud/spring-cloud-stream/tree/master/spring-cloud-stream-integration-tests 并且 Rabbit 和 Kafka 本身也位于单独的模块中以进行自己的集成测试。