-
@Retention(RUNTIME) @Target({METHOD,TYPE}) @Inherited @Repeatable(SetEnvironmentVariables.class) @ExtendWith(EnvironmentVariableExtension.class) public @interface SetEnvironmentVariable@SetEnvironmentVariableis a JUnit Jupiter extension to set the value of an environment variable for a test execution.The key and value of the environment variable to be set must be specified via
key()andvalue(). After the annotated method has been executed, the original value or the value of the higher-level container is restored.SetEnvironmentVariablecan be used on the method and on the class level. It is repeatable and inherited from higher-level containers. If a class is annotated, the configured property will be set before every test inside that class. Any method-level configurations will override the class-level configurations.WARNING: Java considers environment variables to be immutable, so this extension uses reflection to change them. This requires that the
SecurityManagerallows modifications and can potentially break on different operating systems and Java versions. Be aware that this is a fragile solution and consider finding a better one for your specific situation. If you're running on Java 9 or later and are encountering warnings or errors, check the documentation.During parallel test execution, all tests annotated with
ClearEnvironmentVariable,SetEnvironmentVariable,ReadsEnvironmentVariable, andWritesEnvironmentVariableare scheduled in a way that guarantees correctness under mutation of shared global state.For more details and examples, see the documentation on
@ClearEnvironmentVariableand@SetEnvironmentVariable.- Since:
- 0.6