org.junit.contrib.java.lang.system
Class EnvironmentVariables

java.lang.Object
  extended by 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 within 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"));
   }
 }
 

You can ensure that some environment variables are no 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.


Constructor Summary
EnvironmentVariables()
           
 
Method Summary
 org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
           
 void clear(String... names)
          Delete multiple environment variables.
 void set(String name, String value)
          Set the value of an environment variable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EnvironmentVariables

public EnvironmentVariables()
Method Detail

set

public void 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.

clear

public void clear(String... names)
Delete multiple environment variables.

Parameters:
names - the environment variables' names.

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


Copyright © 2011–2018. All rights reserved.