Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ public class EnvironmentVariables implements TestRule {
* @return the rule itself.
*/
public EnvironmentVariables set(String name, String value) {
writeVariableToBuffer(name, value);
if (statementIsExecuting)
writeVariableToEnvMap(name, value);
else
writeVariableToBuffer(name, value);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.junit.contrib.java.lang.system;

import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

import static java.lang.System.getenv;
import static org.assertj.core.api.Assertions.assertThat;

/**
* Reproduces <a href="https://github.com/stefanbirkner/system-rules/issues/86">#86</a>.
*/
@RunWith(Enclosed.class)
public class EnvironmentVariablesClearAcrossTestsTest {
/**
* Variables set in {@code @Before} can be cleared in one test method without
* being restored from the rule's buffer in later tests.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public static class clearInBeforeConfiguredTestClass {
@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables()
.set("someValue", "fromRuleInitializer");

@Before
public void setUp() {
environmentVariables.set("someValue", "testLink");
}

@Test
public void test01_usesEnvironmentVariableFromBefore() {
assertThat(getenv("someValue")).isEqualTo("testLink");
}

@Test
public void test02_clearsEnvironmentVariableSetInBefore() {
environmentVariables.clear("someValue");
assertThat(getenv("someValue")).isNull();
}

@Test
public void test03_environmentVariableIsSetAgainAfterClearTest() {
assertThat(getenv("someValue")).isEqualTo("testLink");
}
}

/**
* The rule instance is shared across tests (same pattern as configuring common
* variables on the rule). Without syncing {@code clear()} to the buffer, a
* variable cleared in one test is restored from the buffer when a later test
* starts.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public static class clearedVariableIsNotRestoredFromBuffer {
private static final EnvironmentVariables SHARED_ENVIRONMENT_VARIABLES
= new EnvironmentVariables().set("someValue", "fromRuleInitializer");

@Rule
public final EnvironmentVariables environmentVariables = SHARED_ENVIRONMENT_VARIABLES;

@Test
public void test01_setVariable() {
environmentVariables.set("someValue", "testLink");
assertThat(getenv("someValue")).isEqualTo("testLink");
}

@Test
public void test02_clearVariable() {
environmentVariables.clear("someValue");
assertThat(getenv("someValue")).isNull();
}

@Test
public void test03_clearedVariableIsNotRestoredFromBuffer() {
assertThat(getenv("someValue")).isNull();
}
}
}