Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring Boot集成测试中读取生成的属性文件?_Java_Spring_Maven_Spring Boot_Spring Test - Fatal编程技术网

Java 如何在Spring Boot集成测试中读取生成的属性文件?

Java 如何在Spring Boot集成测试中读取生成的属性文件?,java,spring,maven,spring-boot,spring-test,Java,Spring,Maven,Spring Boot,Spring Test,在Spring Boot web应用程序中,我使用git提交id插件Maven插件生成一个名为git.properties的文件,其中包含所有git提交信息,例如: git.commit.id=35ca97298544d4ee6f8a5392211ebaa0d9bdafeb 此文件直接在target/classes存储库中生成。所以它包含在类路径中。在运行时,通过我的主应用程序类上的注释加载文件: @PropertySource({"git.properties"}) 然后,我可以在我的be

在Spring Boot web应用程序中,我使用
git提交id插件
Maven插件生成一个名为
git.properties
的文件,其中包含所有git提交信息,例如:

git.commit.id=35ca97298544d4ee6f8a5392211ebaa0d9bdafeb
此文件直接在
target/classes
存储库中生成。所以它包含在类路径中。在运行时,通过我的主应用程序类上的注释加载文件:

@PropertySource({"git.properties"})
然后,我可以在我的bean中使用表达式来获取包含在
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
存储库中。我仍然有相同的错误。

默认情况下,测试运行程序查找与测试文件夹相关的资源。例如,当
git.properties
文件出现在
src/test/resources
中时,它也应该工作


@PropertySource({“classpath:git.properties”})
告诉您从整个类路径中查找源。

@PropertySource({“classpath:git.properties”})
也许吧。太棒了!它起作用了。你能把它作为一个答案,这样我就可以接受它了吗?如果你能解释一下它为什么会起作用,那就好了:)