The @ClearSystemProperty and @SetSystemProperty annotations can be used to clear, respectively, set the values of system properties for a test execution. Both annotations work on the test method and class level, are repeatable as well as combinable. After the annotated method has been executed, the properties mentioned in the annotation will be restored to their original value or will be cleared if they didn’t have one before. Other system properties that are changed during the test, are not restored.

For example, clearing a system property for a test execution can be done as follows:

@Test
@ClearSystemProperty(key = "some property")
void test() {
	assertThat(System.getProperty("some property")).isNull();
}

And setting a system property for a test execution:

@Test
@SetSystemProperty(key = "some property", value = "new value")
void test() {
	assertThat(System.getProperty("some property")).isEqualsTo("new value");
}

As mentioned before, both annotations are repeatable and they can also be combined:

@Test
@ClearSystemProperty(key = "1st property")
@ClearSystemProperty(key = "2nd property")
@SetSystemProperty(key = "3rd property", value = "new value")
void test() {
	assertThat(System.getProperty("1st property")).isNull();
	assertThat(System.getProperty("2nd property")).isNull();
	assertThat(System.getProperty("3rd property")).isEqualsTo("new value");
}

Note that class level configurations are overwritten by method level configurations:

@ClearSystemProperty(key = "some property")
class MySystemPropertyTest {
	@Test
	@SetSystemProperty(key = "some property", value = "new value")
	void test() {
		assertThat(System.getProperty("some property")).isEqualsTo("new value");
	}
}