Annotation Type RestoreSystemProperties


  • @Retention(RUNTIME)
    @Target({METHOD,TYPE})
    @Inherited
    @ExtendWith(SystemPropertyExtension.class)
    public @interface RestoreSystemProperties
    @RestoreSystemProperties is 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 / @BeforeEach blocks. To simply set or clear a system property, consider @SetSystemProperty or @ClearSystemProperty instead.

    RestoreSystemProperties can 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 @BeforeEach blocks in scope and before any @SetSystemProperty or @ClearSystemProperty annotations on that method. After the test, system properties are restored from the snapshot after any @AfterEach have completed.

    When placed on a test class, a snapshot of system properties is stored prior to any @BeforeAll blocks in scope and before any @SetSystemProperty or @ClearSystemProperty annotations on that class. After the test class completes, system properties are restored from the snapshot after any @AfterAll blocks have completed. In addition, a class level annotation is inherited by each test method just as if each one was annotated with RestoreSystemProperties.

    During parallel test execution, all tests annotated with RestoreSystemProperties, SetSystemProperty, ReadsSystemProperty, and WritesSystemProperty are 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 Properties with inherited / default values. Within the context of an element annotated with RestoreSystemProperties, 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