[英]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.