簡體   English   中英

運行測試時如何指示 Spring Boot 使用測試資源中的 application.properties 文件

[英]How to instruct Spring Boot to use application.properties file in test's resources when running tests

對於 Spring 引導應用程序,我有以下項目結構。

在此處輸入圖像描述

問題是,每當我運行測試時,Spring 引導應用程序使用項目根目錄中config文件夾中的application.properties ,而不是src > test > resources文件夾中的application.properties

因此,作為運行測試時的黑客,我在config文件夾中打亂了application.properties的名稱。

我如何告訴 Spring Boot 在運行測試時使用測試位置中的application.properties而無需觸摸config > application.properties

我不想根據我是運行應用程序還是運行測試來繼續重命名文件。

您可以在 src/main/resources 文件夾下創建一個配置文件 ( application-test.properties ),然后在您的測試中使用@TestPropertySource(locations = "classpath:application-test.properties")

使用您的測試配置在 src/main/resources 下創建一個 application-test.properties。

比在你的測試中使用如下

@ActiveProfiles("test")

嘗試這樣做,它可以解決你的問題貨件標記

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM