Java 在 Eclipse 上测试时如何传递 -D 系统属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/862391/
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
How to pass the -D System properties while testing on Eclipse?
提问by Devang Kamdar
I am developing on Eclipse on Windows and Code gets deployed on Unix. I am fetching the system property values using System.getProperty("key") ... How do I pass this in Eclipse so that I do not have to modify the code and it works on Eclipse for debugging?
我正在 Windows 上的 Eclipse 上进行开发,而代码部署在 Unix 上。我正在使用 System.getProperty("key") 获取系统属性值......我如何在 Eclipse 中传递它,这样我就不必修改代码并且它可以在 Eclipse 上进行调试?
Any suggestions?
有什么建议?
采纳答案by Bombe
Run -> Run configurations, select project, second tab: “Arguments”. Top box is for your program, bottom box is for VM arguments, e.g. -Dkey=value
.
运行 -> 运行配置,选择项目,第二个选项卡:“参数”。顶部框用于您的程序,底部框用于 VM 参数,例如-Dkey=value
.
回答by izb
You can add command line arguments to your run configuration. Just edit the run configuration and add -Dmyprop=value (or whatever) to the VM Arguments Box.
您可以将命令行参数添加到运行配置中。只需编辑运行配置并将 -Dmyprop=value(或其他)添加到 VM 参数框。
回答by harry.huang
run configuration -> arguments -> vm arguments
运行配置 -> 参数 -> vm 参数
(can also be placed in the debug configuration under Debug Configuration->Arguments->VM Arguments)
(也可以放在Debug Configuration->Arguments->VM Arguments下的debug配置中)
回答by madx
You can use java System.properties
, for using them from eclipse you could:
您可以使用 java System.properties
,为了从 eclipse 中使用它们,您可以:
- Add
-Dlabel="label_value"
in the VM arguments of the testRun Configuration
like this:
- 添加
-Dlabel="label_value"
测试的 VM 参数,Run Configuration
如下所示:
Then run the test:
import org.junit.Test; import static org.junit.Assert.assertEquals; public class Main { @Test public void test(){ System.out.println(System.getProperty("label")); assertEquals("label_value", System.getProperty("label")); } }
Finally it should pass the test and output this in the console:
label_value
然后运行测试:
import org.junit.Test; import static org.junit.Assert.assertEquals; public class Main { @Test public void test(){ System.out.println(System.getProperty("label")); assertEquals("label_value", System.getProperty("label")); } }
最后它应该通过测试并在控制台中输出:
label_value
回答by NiteshJain007
Yes this is the way:
是的,这是方法:
Right click on your program, select run -> run configuration then on vm argument
右键单击您的程序,选择运行 -> 运行配置,然后选择 vm 参数
-Denv=EnvironmentName -Dcucumber.options="--tags @ifThereisAnyTag"
Then you can apply and close.
然后您可以申请并关闭。
回答by Aniruddha Ghanekar
This will work for junit. for TestNG use following command
这将适用于junit。对于 TestNG 使用以下命令
-ea -Dmykey="value" -Dmykey2="value2"