如何在 JUnit 5 下进行 Spring Boot Test 解析 YAML 文件上外部化的缩进/分层属性

the*_*iro 6 java spring-boot junit5 spring-boot-test

如何在 YAML 文件上解析缩进/分层属性Spring Boot TestJUnit 5

我想编写测试来验证一些依赖于Environment.getProperty(String key)的逻辑:

@ExtendWith(SpringExtension.class)
class PropertiesReolution_SO_IT {

    @Nested
    @TestPropertySource(locations = "classpath:application-test.yml")
    public class ViaYamlFile {

        @Autowired
        private Environment env;

        @Test
        void testGetDottedHierarchicalProperty() throws Exception {
            final String key = "dotted.hierarchical.property";
            assertNotNull(this.env.getProperty(key));
            assertEquals("application-test.yml", this.env.getProperty(key));
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

dotted.hierarchical.property属性在 YAML 文件中定义application-test.yml如下:

@ExtendWith(SpringExtension.class)
class PropertiesReolution_SO_IT {

    @Nested
    @TestPropertySource(locations = "classpath:application-test.yml")
    public class ViaYamlFile {

        @Autowired
        private Environment env;

        @Test
        void testGetDottedHierarchicalProperty() throws Exception {
            final String key = "dotted.hierarchical.property";
            assertNotNull(this.env.getProperty(key));
            assertEquals("application-test.yml", this.env.getProperty(key));
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

测试用例失败,属性评估为null。我被锁定到 spring-boot 1.5.8.RELEASE,所以我使用org.springframework.test:spring-test-junit5。我用完整的例子创建了一个要点