[英]Spring Boot properties in 'application.yml' not loading from JUnit Test
[英]Spring Boot is not loading the properties from a YML file
我的测试文件是:
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyApplicationTest {
@InjectMocks
@Autowired
protected MyController myController;
}
我的Controller
文件是:
@RestController
@RequestMapping(value="${app.base-path}") // Unable to load this property
public class MyController {
}
我试过@TestPropertySource
但没有用。 SpringBootApplication
工作正常。 @Value
注释不是这种情况。 用@Value
注释装饰的字段工作得很好。
您是否将 .yml 文件命名为application.yml
并将其放在src/main/resources
或src/test/resources
?
将 application.yml 文件放在这些文件夹中会自动将其添加到类路径中,以便您可以在运行时访问此文件属性。
默认情况下,Spring Boot 将尝试加载 classpath 上可用的application.yml
和application-{profilename}.yml
,因此您可以尝试使用application-test.yml
(它将消除在一个文件中定义配置文件的问题 - 也许它会导致你的问题)。
此外,您可以尝试使用bootstrap.yml
,它在application.yml
之前加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.