public class ProvideSystemProperty
extends org.junit.rules.ExternalResource
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");
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"); ... }
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,java.lang.String> |
properties |
private RestoreSpecificSystemProperties |
restoreSystemProperty |
Constructor and Description |
---|
ProvideSystemProperty()
Deprecated.
|
ProvideSystemProperty(java.lang.String name,
java.lang.String value) |
Modifier and Type | Method and Description |
---|---|
private void |
addProperty(java.lang.String name,
java.lang.String value) |
protected void |
after() |
ProvideSystemProperty |
and(java.lang.String name,
java.lang.String value) |
protected void |
before() |
static ProvideSystemProperty |
fromFile(java.lang.String name) |
private static ProvideSystemProperty |
fromInputStream(java.io.InputStream is) |
static ProvideSystemProperty |
fromResource(java.lang.String name) |
private void |
setProperties() |
void |
setProperty(java.lang.String name,
java.lang.String value)
Deprecated.
Please use
RestoreSystemProperties
along with System.setProperty(String, String) . |
private final java.util.Map<java.lang.String,java.lang.String> properties
private final RestoreSpecificSystemProperties restoreSystemProperty
@Deprecated public ProvideSystemProperty()
setProperty(String, String)
.public ProvideSystemProperty(java.lang.String name, java.lang.String value)
public static ProvideSystemProperty fromFile(java.lang.String name)
public static ProvideSystemProperty fromResource(java.lang.String name)
private static ProvideSystemProperty fromInputStream(java.io.InputStream is) throws java.io.IOException
java.io.IOException
@Deprecated public void setProperty(java.lang.String name, java.lang.String value)
RestoreSystemProperties
along with System.setProperty(String, String)
.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"); ... }
name
- the name of the property.value
- the new value of the property.public ProvideSystemProperty and(java.lang.String name, java.lang.String value)
private void addProperty(java.lang.String name, java.lang.String value)
protected void before() throws java.lang.Throwable
before
in class org.junit.rules.ExternalResource
java.lang.Throwable
private void setProperties()
protected void after()
after
in class org.junit.rules.ExternalResource