Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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
Unit testing 如何定义@TestPropertySource(位置)?_Unit Testing_Spring Boot - Fatal编程技术网

Unit testing 如何定义@TestPropertySource(位置)?

Unit testing 如何定义@TestPropertySource(位置)?,unit-testing,spring-boot,Unit Testing,Spring Boot,我正在尝试在测试包中添加一个test.properties文件。 结构是 test->java-> -> test.java -> resource -> test.properties 我的命令是 @RunWith(SpringRunner.class) @ContextConfiguration(classes = Publish.class) @TestPropertySource(locations =

我正在尝试在测试包中添加一个
test.properties
文件。 结构是

test->java->  -> test.java
              -> resource -> test.properties
我的命令是

   @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = Publish.class)
    @TestPropertySource(locations = "classpath:/src/test/java/resources/test.properties")
    public class Test {


...
}
但我总是这样

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.IllegalStateException: Failed to add PropertySource to Environment
Caused by: java.io.FileNotFoundException: class path resource [/src/test/java/resources/test.properties] cannot be opened because it does not exist
我有几个其他变种,我仍然得到这个错误,我如何才能解决这个

其他变体:

 @TestPropertySource(locations = "classpath:/test.properties")

 @TestPropertySource(locations = "classpath:/resources/test.properties")

 @TestPropertySource(locations = "classpath:test.properties")
有人问了另一个类似的问题,但我似乎没有解决我的问题

 @TestPropertySource(locations = "classpath:/yourPackage/thatContainResource/test.properties")
src
不在运行时,并且不是类路径的一部分

但最重要的是,您的目录结构是错误的<代码>资源应该在
java
旁边,如下所示:


通过将资源放入源代码中(就像您所做的那样),它们被过滤掉了。

contrak/contrak/src/test/java/resources/test.properties
是一种自我解释。您没有这样的结构(从contrak/contrak开始),您的意思是
/resource/test.properties
???如果我使用
test->resource->test.properties
structure??不,那么它将只是
test.properties
。从
resources
目录中的所有内容都放在bundle中(例如放在ZIP中),使其在类路径上可用。是否可以更改结构并使其仍然工作??您可以尝试修改一些maven配置-但为什么要这样做?99.9%的maven项目看起来是这样的。只是好奇,我尝试使用另一个结构
test->awesome->anothertest.properties
来查看它是否可以工作,但实际上不行,似乎任何资源都必须在资源文件夹中