java 使用 SpringJunit4ClassRunner 在属性占位符之前设置系统属性或环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10822010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set System Properties or Environment Variables Before Property Placeholder with SpringJunit4ClassRunner
提问by andy
I have a main app-context.xml that defines a property placeholder with two locations: default properties file and an optional override file:
我有一个主要的 app-context.xml,它定义了一个具有两个位置的属性占位符:默认属性文件和一个可选的覆盖文件:
<context:property-placeholder
location="classpath:config.properties,${configOverride}"
ignore-resource-not-found="true" />
The optional override location allows specifying another properties file (e.g. "-DconfigOverride=file:/home/app/config.properties") with only the properties that should be overridden.
可选的覆盖位置允许仅使用应覆盖的属性指定另一个属性文件(例如“-DconfigOverride=file:/home/app/config.properties”)。
For my unit tests, I'm using a test context that imports app-context.xml:
对于我的单元测试,我使用了一个导入 app-context.xml 的测试上下文:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-context.xml"})
public class UserServiceTest {
...
}
How can I set system properties or environment variables within the application before the application context is loaded? I would like to achieve the same effect as setting "-DconfigOverride=classpath:testConfig.properties" across all test classes without having to specify a command line arg, if possible.
如何在加载应用程序上下文之前在应用程序中设置系统属性或环境变量?如果可能,我想在所有测试类中实现与设置“-DconfigOverride=classpath:testConfig.properties”相同的效果,而无需指定命令行参数。
采纳答案by Ahamed Mustafa M
Thinking of ,
想着,
- Extending
SpringJUnit4ClassRunner
and setting the system propertyconfigOverride
in its constructor/initialization block - Then passing
ExtendedSpringJUnit4ClassRunner
to@RunWith
- 在其构造函数/初始化块中扩展
SpringJUnit4ClassRunner
和设置系统属性configOverride
- 然后传递
ExtendedSpringJUnit4ClassRunner
到@RunWith
回答by kevin
One other alternative is setting the environment property in a @BeforeClass annotated method, which will be invoked before the Context Configuration happens.
另一种选择是在@BeforeClass 注释方法中设置环境属性,该方法将在上下文配置发生之前调用。
@BeforeClass
public static void setSystemProps() {
System.setProperty("configOverride", "yourVal");
}
回答by andy
Here's what I ended up doing - I didn't have to change any unit test classes. Unfortunately, I didn't set the "configOverride" property (see AhamedMustafaM's answer for one way to do that) and instead went with overriding the original property placeholder definition (I tried again after my initial failed attempts and got it to work).
这就是我最终要做的 - 我不必更改任何单元测试类。不幸的是,我没有设置“configOverride”属性(请参阅 AhamedMustafaM 的答案以了解一种方法),而是覆盖了原始属性占位符定义(我在最初的尝试失败后再次尝试并使其工作)。
I added the following line to my testContext.xml:
我将以下行添加到我的 testContext.xml 中:
<!-- import the main app context -->
<import resource="classpath:appContext.xml" />
<!-- this is the line i added -->
<context:property-placeholder order="-999"
location="classpath:testConfig.properties"
ignore-unresolvable="true" />
Note the order="-999" attribute, which is used to ensure priority over the original property-placeholder definition(s). Also I set "ignore-unresolvable" to "true" to delegate any unresolvable properties to the original placeholder configurer.
请注意 order="-999" 属性,该属性用于确保优先于原始属性占位符定义。此外,我将“ignore-unresolvable”设置为“true”以将任何无法解析的属性委托给原始占位符配置器。
回答by Sled
My issue was similar but I wanted to set the spring.profiles.active
environment variable and it turned out that I just needed to throw @ActiveProfiles()
with the values I wanted on to the test itself.
我的问题很相似,但我想设置spring.profiles.active
环境变量,结果我只需要将@ActiveProfiles()
我想要的值扔给测试本身。