Class EnvironmentVariables

java.lang.Object
org.junit.contrib.java.lang.system.EnvironmentVariables
All Implemented Interfaces:
org.junit.rules.TestRule

public class EnvironmentVariables extends Object implements org.junit.rules.TestRule
The EnvironmentVariables rule allows you to set environment variables for your test. All changes to environment variables are reverted after the test.
 public class EnvironmentVariablesTest {
   @Rule
   public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

   @Test
   public void test() {
     environmentVariables.set("name", "value");
     assertEquals("value", System.getenv("name"));
   }
 }
 

Common variables can be set directly after creating the rule

 public class EnvironmentVariablesTest {
   @Rule
   public final EnvironmentVariables environmentVariables = new EnvironmentVariables()
       .set("name", "value");

   @Test
   public void test() {
     assertEquals("value", System.getenv("name"));
   }
 }
 

You can ensure that some environment variables are not set by calling clear(String...).

Warning: This rule uses reflection for modifying internals of the environment variables map. It fails if your SecurityManager forbids such modifications.

  • Field Details

    • buffer

      private final Map<String,String> buffer
    • statementIsExecuting

      private boolean statementIsExecuting
  • Constructor Details

    • EnvironmentVariables

      public EnvironmentVariables()
  • Method Details

    • set

      public EnvironmentVariables set(String name, String value)
      Set the value of an environment variable.
      Parameters:
      name - the environment variable's name.
      value - the environment variable's new value. May be null.
      Returns:
      the rule itself.
    • clear

      public EnvironmentVariables clear(String... names)
      Delete multiple environment variables.
      Parameters:
      names - the environment variables' names.
      Returns:
      the rule itself.
    • writeVariableToEnvMap

      private void writeVariableToEnvMap(String name, String value)
    • set

      private void set(Map<String,String> variables, String name, String value)
    • writeVariableToBuffer

      private void writeVariableToBuffer(String name, String value)
    • copyVariablesFromBufferToEnvMap

      private void copyVariablesFromBufferToEnvMap()
    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
      Specified by:
      apply in interface org.junit.rules.TestRule
    • getEditableMapOfVariables

      private static Map<String,String> getEditableMapOfVariables()
    • getTheCaseInsensitiveEnvironment

      private static Map<String,String> getTheCaseInsensitiveEnvironment()
    • getFieldValue

      private static Map<String,String> getFieldValue(Class<?> klass, Object object, String name) throws NoSuchFieldException, IllegalAccessException
      Throws:
      NoSuchFieldException
      IllegalAccessException