Class EnvironmentVariableHelper

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class EnvironmentVariableHelper
    extends org.junit.rules.ExternalResource
    A utility that can temporarily forcibly set environment variables and then allows resetting them to the original values. This only works for environment variables read by the SDK.
    • Constructor Detail

      • EnvironmentVariableHelper

        public EnvironmentVariableHelper()
    • Method Detail

      • remove

        public void remove​(String key)
      • reset

        public void reset()
      • after

        protected void after()
        Overrides:
        after in class org.junit.rules.ExternalResource
      • run

        public static void run​(Consumer<EnvironmentVariableHelper> helperConsumer)
        Static run method that allows for "single-use" environment variable modification. Example use:
         
         EnvironmentVariableHelper.run(helper -> {
            helper.set("variable", "value");
            //run some test that uses "variable"
         });
         
         
        Will call reset() at the end of the block (even if the block exits exceptionally).
        Parameters:
        helperConsumer - a code block to run that gets an EnvironmentVariableHelper as an argument