在 Eclipse 上测试时如何传递-D System 属性?

我正在 Windows 上的 Eclipse 上开发,并且在 Unix 上部署了 Code。我正在使用 System.getProperty (“ key”)获取系统属性值... ... 如何在 Eclipse 中传递这个属性值,以便不必修改代码,并且它可以在 Eclipse 上进行调试?

有什么建议吗?

178273 次浏览

Run -> Run configurations, select project, second tab: “Arguments”. Top box is for your program, bottom box is for VM arguments, e.g. -Dkey=value.

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.

You can use java System.properties, for using them from eclipse you could:

  1. Add -Dlabel="label_value" in the VM arguments of the test Run Configuration like this:

eclipse_vm_config

  1. 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"));
    }
    }
    
  2. Finally it should pass the test and output this in the console:

    label_value
    

Yes this is the way:

Right click on your program, select run -> run configuration then on vm argument

-Denv=EnvironmentName -Dcucumber.options="--tags @ifThereisAnyTag"

Then you can apply and close.

This will work for junit. for TestNG use following command

-ea -Dmykey="value" -Dmykey2="value2"