简体   繁体   English

Spring @TestPropertySource类路径vs文件

[英]Spring @TestPropertySource classpath vs file

Is there way to load test properties like this? 有没有办法加载像这样的测试属性?

@TestPropertySource("${test.config:'classpath:application.test.properties'}")

The point is to check test.config argument (there could be something like file:c:/test.properties ) and if the value is not set, then it will take default properties from classpath. 关键是检查test.config参数(可能有类似file:c:/test.properties ),如果没有设置该值,那么它将从classpath获取默认属性。

I was able to load properties from classpath swithing it using argument. 我能够使用参数从classpath加载属性。

I was unable to combine classpath and file 我无法将classpath和文件组合在一起

Solved: 解决了:

@PropertySource(value = {"classpath:application.test.properties","${test.config}"}, ignoreResourceNotFound = true)
  • if environment variable test.config is not defined then context will not be loaded 如果未定义环境变量test.config,则不会加载上下文
  • if environment variable test.config is defined but file test.properties is not present then also context will loaded. 如果定义了环境变量test.config但是文件test.properties不存在,那么也将加载上下文。

I think you are not using TestPropertySource as per it's use case. 我认为你没有使用TestPropertySource作为它的用例。

TestPropertySource is used if you want to override some properties during test cases. 如果要在测试用例期间覆盖某些属性,则使用TestPropertySource If you want to override some properties in test cases, just put those properties in test property file. 如果要在测试用例中覆盖某些属性,只需将这些属性放在测试属性文件中即可。 Spring will override only those properties which are there in in test property file Spring将仅覆盖测试属性文件中的那些属性

Not sure why you want to check test.config environment variable and then do some operations . 不确定为什么要检查test.config环境变量然后执行一些操作。

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

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