stefanbirkner / system-rules

A collection of JUnit rules for testing code which uses java.lang.System.
http://stefanbirkner.github.io/system-rules
Other
546 stars 71 forks source link

Environment variables not reset between classes #61

Closed MarkRx closed 6 years ago

MarkRx commented 6 years ago

Environment variables should be reset after each class. Otherwise setup from one class leaks into another class which causes inconsistent behavior.

Example:

A.java:

public class A {
      @Rule
      public EnvironmentVariables environmentVariables = new EnvironmentVariables();
      {
          environmentVariables.set("A", "A");
      };

      @Test
      public void test1() {
          assertEquals("A", System.getenv("A"));
          assertEquals(null, System.getenv("B"));
      }
}

B.java:

public class B {
      @Rule
      public EnvironmentVariables environmentVariables = new EnvironmentVariables();
      {
          environmentVariables.set("B", "B");
      };

      @Test
      public void test1() {
          assertEquals(null, System.getenv("A"));
          assertEquals("B", System.getenv("B"));
      }
}
stefanbirkner commented 6 years ago

Thanks for reporting the bug. I can reproduce it and work on a fix.

stefanbirkner commented 6 years ago

Currently the methods of EnvironmentVariables are not designed to be used outside of an @Before or the test itself. If you put the environmentVariables.set("B", "B") into an @Before method then everything works fine.

Nevertheless I consider the current behaviour as a bug and will fix it.

stefanbirkner commented 6 years ago

This is fixed in System Rules 1.18.0: 063b1c82864c11b9dd3e14846665f796f340b7d5