-
@Retention(RUNTIME) @Target({METHOD,TYPE}) @Inherited @ExtendWith(SystemPropertyExtension.class) public @interface RestoreSystemProperties@RestoreSystemPropertiesis a JUnit Jupiter extension to restore the entire set of system properties to the original value, or the value of the higher-level container, after the annotated element is complete.Use this annotation when there is a need programmatically modify system properties in a test method or in
@BeforeAll/@BeforeEachblocks. To simply set or clear a system property, consider@SetSystemPropertyor@ClearSystemPropertyinstead.RestoreSystemPropertiescan be used on the method and on the class level. When placed on a test method, a snapshot of system properties is stored prior to that test. The snapshot is created before any@BeforeEachblocks in scope and before any@SetSystemPropertyor@ClearSystemPropertyannotations on that method. After the test, system properties are restored from the snapshot after any@AfterEachhave completed.When placed on a test class, a snapshot of system properties is stored prior to any
@BeforeAllblocks in scope and before any@SetSystemPropertyor@ClearSystemPropertyannotations on that class. After the test class completes, system properties are restored from the snapshot after any@AfterAllblocks have completed. In addition, a class level annotation is inherited by each test method just as if each one was annotated withRestoreSystemProperties.During parallel test execution, all tests annotated with
RestoreSystemProperties,SetSystemProperty,ReadsSystemProperty, andWritesSystemPropertyare scheduled in a way that guarantees correctness under mutation of shared global state.For more details and examples, see the documentation on
@ClearSystemProperty,@SetSystemProperty, and@RestoreSystemProperties.Note: System properties are normally just a hashmap of strings, however, it is technically possible to store non-string values and create nested
Propertieswith inherited / default values. Within the context of an element annotated withRestoreSystemProperties, non-String values are not preserved and the structure of nested defaults are flattened. After the annotated context is exited, the original Properties object is restored with all its potential (non-standard) richness.- Since:
- 2.0.0