Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

eclipse

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?

like image 571
Devang Kamdar Avatar asked May 14 '09 09:05

Devang Kamdar


People also ask

How do you override a system property in Java?

Do you want to overwrite a system property? You can do this. final String propertyName = "Property"; String oldProperty = System. getProperty(propertyName); System.

How do I set system properties in JUnit?

If your test relies on system properties you could set them and unset them in 'before' and 'after' lifecycle methods. In Junit5, setting system properties for all tests in a test case might look like this: @BeforeAll public static void setSystemProperties() { // set the system properties // ... }

How do I set system properties in Java?

Programmatically, a system property can be set using the setProperty method of the System object, and also via the setProperty method of the Properties object that can be obtained from System via getProperties.


2 Answers

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

like image 106
Bombe Avatar answered Oct 06 '22 12:10

Bombe


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 
like image 41
madx Avatar answered Oct 06 '22 12:10

madx