How to pass the -D System properties while testing on Eclipse?
Asked Answered
A

5

101

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?

Any suggestions?

Altercation answered 14/5, 2009 at 9:26 Comment(0)
A
142

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

Aconite answered 14/5, 2009 at 9:29 Comment(6)
where is this if you are trying to run an android application?Horowitz
why do I have to use 'D' as prefix?Roush
If you're as thick as me -- -D arguments go in the bottom box for VM arguments, not in the top box for your program's args. /sighBurnight
The top box is for the String[] args passed to the main method.Whin
Is there any way to set this to happen by default, so you don't have to manually enter this repeatedly for many tests?Bithynia
value is something in quotes?Quash
Q
53

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
    
Quadriceps answered 19/5, 2016 at 12:56 Comment(0)
T
13

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.

Troche answered 14/5, 2009 at 9:29 Comment(1)
If you meant "Program Arguments box" when you said "command-line args box" under Arguments tab - Does not Work! Has to be entered in "VM Arguments Box" as mentioned by Bombe above.Altercation
W
4

This will work for junit. for TestNG use following command

-ea -Dmykey="value" -Dmykey2="value2"
Willie answered 20/8, 2019 at 12:44 Comment(0)
M
0

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.

Misname answered 1/3, 2019 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.