在春季啓動Web應用程序,我用的是git-commit-id-plugin
Maven插件生成一個名爲git.properties
文件,包含所有的git的承諾信息,e.g:如何在Spring Boot集成測試中讀取生成的屬性文件?
git.commit.id=35ca97298544d4ee6f8a5392211ebaa0d9bdafeb
此文件在target/classes
庫中直接生成。所以它包含在classpath中。在運行時,文件是通過註釋裝上我的主應用程序類:
@PropertySource({"git.properties"})
然後我可以使用表達式在我的豆子得到包含在git.properties
文件屬性的值:
@Value("${git.commit.id}")
private String gitCommitIdFull; // will contain "35ca97298544d4ee6f8a5392211ebaa0d9bdafeb"
這一切正常運行應用程序時運行得很好。
但現在我試圖運行與運行一些集成測試:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleSearchDAOTest {
//tests here...
}
我得到以下異常:
java.lang.IllegalStateException: Failed to load ApplicationContext
(...)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to parse configuration class [ch.cscf.mds.MdsApiApplication];
nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/git.properties]
顯然,測試運行的方式,他們似乎不要使用target/classes
作爲類路徑的基礎。
它有什麼用?如何使這些測試的運行時知道target/classes/git.properties
文件?
我試圖生成git.properties
文件到src/main/resources
目錄而不是target/classes
存儲庫。我仍然有同樣的錯誤。
'@PropertySource({「classpath:git.properties」})'也許。 – Vaelyr
非常好!有用。你可以把它作爲答案,我可以接受嗎? –
有些解釋爲什麼它的作品也會很好:) –