org.junit.contrib.java.lang.system
Class ProvideSystemProperty

java.lang.Object
  extended by org.junit.rules.ExternalResource
      extended by org.junit.contrib.java.lang.system.ProvideSystemProperty
All Implemented Interfaces:
org.junit.rules.TestRule

public class ProvideSystemProperty
extends org.junit.rules.ExternalResource

The ProvideSystemProperty rule provides an arbitrary value for a system property to a test. After the test the original value is restored. You can ensure that a property is not set by providing null (or using ClearSystemProperties).

Let's assume the system property MyProperty is not set and the system property OtherProperty has the value OtherValue. Now run the test

   public void MyTest {
     @Rule
     public final ProvideSystemProperty provideSystemProperty
       = new ProvideSystemProperty("MyProperty", "MyValue")
         .and("OtherProperty", null);

     @Test
     public void overridesProperty() {
       assertEquals("MyValue", System.getProperty("MyProperty"));
     }

     @Test
     public void deletesProperty() {
       assertNull(System.getProperty("OtherProperty"));
     }
   }
 
Both tests succeed and after the tests, the system property MyProperty is not set and the system property OtherProperty has the value OtherValue.

You can use a properties file to supply properties for the ProvideSystemProperty rule. The file can be from the file system or the class path. In the first case use

 @Rule
 public final ProvideSystemProperty properties = ProvideSystemProperty
                .fromFile("/home/myself/example.properties");
 
and in the second case use
 @Rule
 public final ProvideSystemProperty properties = ProvideSystemProperty
                .fromResource("example.properties");
 

Set property for a single test

If you want to set a property for a single test then you can use RestoreSystemProperties along with System.setProperty(String, String).

 @Rule
 public final TestRule restoreSystemProperties
   = new RestoreSystemProperties();

 @Test
 public void test() {
   System.setProperty("YourProperty", "YourValue");
   ...
 }


Constructor Summary
ProvideSystemProperty()
          Deprecated. see setProperty(String, String).
ProvideSystemProperty(String name, String value)
           
 
Method Summary
protected  void after()
           
 ProvideSystemProperty and(String name, String value)
           
protected  void before()
           
static ProvideSystemProperty fromFile(String name)
           
static ProvideSystemProperty fromResource(String name)
           
 void setProperty(String name, String value)
          Deprecated. Please use RestoreSystemProperties along with System.setProperty(String, String).
 
Methods inherited from class org.junit.rules.ExternalResource
apply
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProvideSystemProperty

@Deprecated
public ProvideSystemProperty()
Deprecated. see setProperty(String, String).


ProvideSystemProperty

public ProvideSystemProperty(String name,
                             String value)
Method Detail

fromFile

public static ProvideSystemProperty fromFile(String name)

fromResource

public static ProvideSystemProperty fromResource(String name)

setProperty

@Deprecated
public void setProperty(String name,
                                   String value)
Deprecated. Please use RestoreSystemProperties along with System.setProperty(String, String).

Sets the property with the name to the specified value. After the test the rule restores the value of the property at the point of setting it.

This method is deprecated. If you're still using it, please replace your current code

 @Rule
 public final ProvideSystemProperty provideSystemProperty = new ProvideSystemProperty();

 @Test
 public void test() {
   provideSystemProperty.setProperty("YourProperty", "YourValue");
   ...
 }
with this code:
 @Rule
 public final TestRule restoreSystemProperties = new RestoreSystemProperties();

 @Test
 public void test() {
   System.setProperty("YourProperty", "YourValue");
   ...
 }

Parameters:
name - the name of the property.
value - the new value of the property.
Since:
1.6.0

and

public ProvideSystemProperty and(String name,
                                 String value)

before

protected void before()
               throws Throwable
Overrides:
before in class org.junit.rules.ExternalResource
Throws:
Throwable

after

protected void after()
Overrides:
after in class org.junit.rules.ExternalResource


Copyright © 2011–2018. All rights reserved.