在测试中启用配置 属性 而不加载完整的 Spring 引导应用程序上下文
Enable configuration property in test without loading full Spring Boot application context
假设我们有 Spring 引导应用程序并且只想加载应用程序上下文的特定片段。
特别载入YAML文件配置,并将spring.datasource
映射到@ConfigurationProperties
标记的DataSourceProperties
。
天真的不工作测试声明是:
@RunWith(SpringRunner.class)
@ContextConfiguration(
classes = {DataSourceAutoConfiguration.class, DataSourceProperties.class},
loader = AnnotationConfigContextLoader.class,
initializers = ConfigFileApplicationContextInitializer.class)
@TestPropertySource({"classpath:application.yaml", "classpath:application-dev.yaml"})
@EnableConfigurationProperties({DataSourceProperties.class})
@Slf4j
public class HibernateTest {
@Autowired
private DataSourceProperties dataSourceProperties;
@Test
public void dataSourceTest() throws SQLException {
log.info("DS URL: {}", dataSourceProperties.getUrl());
}
}
application-dev.yaml
有:
spring.datasource:
url: jdbc:oracle:thin:@localhost:1521/APP
测试打印:
DS URL: null
我正在寻找一种方法将 YAML 配置映射到 @ConfigurationProperties
标记为 class (DataSourceProperties
) 并让 @Configuration
class ( DataSourceAutoConfiguration
) 不加载任何其他 services/components/etc...
这些链接回答了我的问题:
- https://jira.spring.io/browse/SPR-13912
将对 YAML 文件的支持添加到 @属性Source(解决方案:未解决)。
- https://jira.spring.io/browse/SPR-16563
记录@属性Source 和@Test属性Source 不支持 YAML。
- https://github.com/spring-projects/spring-boot/issues/12388
属性 覆盖不适用于 YML 文件。
- https://github.com/spring-projects/spring-boot/issues/10772
来自 Test属性Source 的 yaml 中的映射属性在 boot 2.0.0.
中不起作用
简而言之 Spring 框架开发人员不想支持 @TestPropertySource
和 @PropertySource
注释的 YAML 格式,解释了 Spring Boot 中的 YAML 支持以棘手的方式制作/ 断路(虽然我在解释中迷路了)。
切换代码以使用 .properties
文件后,我的测试工作正常。
假设我们有 Spring 引导应用程序并且只想加载应用程序上下文的特定片段。
特别载入YAML文件配置,并将spring.datasource
映射到@ConfigurationProperties
标记的DataSourceProperties
。
天真的不工作测试声明是:
@RunWith(SpringRunner.class)
@ContextConfiguration(
classes = {DataSourceAutoConfiguration.class, DataSourceProperties.class},
loader = AnnotationConfigContextLoader.class,
initializers = ConfigFileApplicationContextInitializer.class)
@TestPropertySource({"classpath:application.yaml", "classpath:application-dev.yaml"})
@EnableConfigurationProperties({DataSourceProperties.class})
@Slf4j
public class HibernateTest {
@Autowired
private DataSourceProperties dataSourceProperties;
@Test
public void dataSourceTest() throws SQLException {
log.info("DS URL: {}", dataSourceProperties.getUrl());
}
}
application-dev.yaml
有:
spring.datasource:
url: jdbc:oracle:thin:@localhost:1521/APP
测试打印:
DS URL: null
我正在寻找一种方法将 YAML 配置映射到 @ConfigurationProperties
标记为 class (DataSourceProperties
) 并让 @Configuration
class ( DataSourceAutoConfiguration
) 不加载任何其他 services/components/etc...
这些链接回答了我的问题:
- https://jira.spring.io/browse/SPR-13912 将对 YAML 文件的支持添加到 @属性Source(解决方案:未解决)。
- https://jira.spring.io/browse/SPR-16563 记录@属性Source 和@Test属性Source 不支持 YAML。
- https://github.com/spring-projects/spring-boot/issues/12388 属性 覆盖不适用于 YML 文件。
- https://github.com/spring-projects/spring-boot/issues/10772 来自 Test属性Source 的 yaml 中的映射属性在 boot 2.0.0. 中不起作用
简而言之 Spring 框架开发人员不想支持 @TestPropertySource
和 @PropertySource
注释的 YAML 格式,解释了 Spring Boot 中的 YAML 支持以棘手的方式制作/ 断路(虽然我在解释中迷路了)。
切换代码以使用 .properties
文件后,我的测试工作正常。