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
org.junit.contrib.java.lang.system.RestoreSystemPropertiesalong with
System#clearProperty(String).
@Rule
public final TestRule restoreSystemProperties
= new RestoreSystemProperties();
@Test
public void test() {
System.clearProperty("YourProperty");
...
}