简体   繁体   English

Spring 引导应用程序从 jar 的属性文件中读取,在运行测试时添加为依赖项

[英]Spring boot application to read from a properties file of a jar added as a dependency while running test

I have a spring boot application and it has another spring application included as a jar in maven dependency.我有一个 spring 启动应用程序,它还有另一个 spring 应用程序作为 jar 包含在 Z402C0709AF6B240F711 依赖项中。 In that jar, i have a properties file under在那个 jar 中,我有一个属性文件

src/main/resources/data.properties

Another file is present for testing存在另一个文件用于测试

src/test/resources/data.properties

Now when i am running my main application test, then i want it to use the file under src/test/resources of that jar.现在,当我运行我的主应用程序测试时,我希望它使用该 jar 的 src/test/resources 下的文件。

I tried doing below in my Test config class.我尝试在我的测试配置 class 中执行以下操作。 But it fails with a file not found exception.但它失败并出现文件未找到异常。 Is there a way i can load the file in the test/resources有没有办法可以在测试/资源中加载文件

@Configuration
@ComponentScan(basePackages = { "com.app" }, excludeFilters = {
@PropertySource(value = {"file:/myApp/src/test/resources/data.properties"})

Instead of loading the properties from a file you could load it from the classpath, which would also solve the issue that for the test, you want to get the one on your test/resources您可以从类路径中加载它,而不是从文件中加载属性,这也可以解决测试的问题,您希望在test/resources中获取该属性

@PropertySource(value = {"classpath:data.properties"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM