Class ClearSystemProperties
java.lang.Object
org.junit.rules.ExternalResource
org.junit.contrib.java.lang.system.ClearSystemProperties
- All Implemented Interfaces:
org.junit.rules.TestRule
public class ClearSystemProperties
extends org.junit.rules.ExternalResource
The
ClearSystemProperties
rule clears a set of system
properties when the test starts and restores their original values
when the test finishes (whether it passes or fails).
Supposing that the system property YourProperty
has the
value YourValue
. Now run the test
public void YourTest { @Rule public final TestRule clearSystemProperties = new ClearSystemProperties("YourProperty"); @Test public void verifyProperty() { assertNull(System.getProperty("YourProperty")); } }The test succeeds and afterwards the system property
YourProperty
has the value YourValue
again.
The ClearSystemProperties
rule accepts a list of
properties in case you need to clear multiple properties:
@Rule public final TestRule clearSystemProperties = new ClearSystemProperties("first", "second", "third");
Clear property for a single test
If you want to clear a property for a single test then you can
use
RestoreSystemProperties
along with System.clearProperty(String)
.
@Rule public final TestRule restoreSystemProperties = new RestoreSystemProperties(); @Test public void test() { System.clearProperty("YourProperty"); ... }
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final String[]
private final RestoreSpecificSystemProperties
-
Constructor Summary
ConstructorsConstructorDescriptionClearSystemProperties
(String... properties) Creates aClearSystemProperties
rule that clears the specified properties and restores their original values when the test finishes (whether it passes or fails). -
Method Summary
Modifier and TypeMethodDescriptionprotected void
after()
protected void
before()
private void
void
clearProperty
(String property) Deprecated.private void
Methods inherited from class org.junit.rules.ExternalResource
apply
-
Field Details
-
restoreSystemProperty
-
properties
-
-
Constructor Details
-
ClearSystemProperties
Creates aClearSystemProperties
rule that clears the specified properties and restores their original values when the test finishes (whether it passes or fails).- Parameters:
properties
- the properties' names.
-
-
Method Details
-
clearProperty
Deprecated.Please useRestoreSystemProperties
along withSystem.clearProperty(String)
.Clears the property and restores the value of the property at the point of clearing it.This method is deprecated. If you're still using it, please replace your current code
@Rule public final ClearSystemProperties clearSystemProperties = new ClearSystemProperties(); @Test public void test() { clearSystemProperties.clearProperty("YourProperty"); ... }
with this code:@Rule public final TestRule restoreSystemProperties = new RestoreSystemProperties(); @Test public void test() { System.clearProperty("YourProperty"); ... }
- Parameters:
property
- the name of the property.- Since:
- 1.6.0
-
before
- Overrides:
before
in classorg.junit.rules.ExternalResource
- Throws:
Throwable
-
after
protected void after()- Overrides:
after
in classorg.junit.rules.ExternalResource
-
clearProperties
private void clearProperties() -
restoreOriginalValue
private void restoreOriginalValue()
-
RestoreSystemProperties
along withSystem.clearProperty(String)
.