BAEL-7006 Create example code for environment variables article

This commit is contained in:
Ashley Frieze
2023-09-23 22:17:39 +01:00
parent 1a9337f238
commit ece337978c
11 changed files with 280 additions and 1 deletions
@@ -0,0 +1,38 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
class EnvironmentVariablesByAbstractionUnitTest {
@FunctionalInterface
interface GetEnv {
String get(String name);
}
static class ReadsEnvironment {
private GetEnv getEnv;
public ReadsEnvironment(GetEnv getEnv) {
this.getEnv = getEnv;
}
public String whatOs() {
return getEnv.get("OS");
}
}
@Test
void givenFakeOs_thenCanReadIt() {
Map<String, String> fakeEnv = new HashMap<>();
fakeEnv.put("OS", "MacDowsNix");
ReadsEnvironment reader = new ReadsEnvironment(fakeEnv::get);
assertThat(reader.whatOs())
.isEqualTo("MacDowsNix");
}
}
@@ -0,0 +1,22 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ClearEnvironmentVariable;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import static org.assertj.core.api.Assertions.assertThat;
@SetEnvironmentVariable(key = "pioneer", value = "is pioneering")
class EnvironmentVariablesSetByJUnitPioneerUnitTest {
@Test
void givenEnvironmentVariableIsSetForClass_thenVariableCanBeRead() {
assertThat(System.getenv("pioneer")).isEqualTo("is pioneering");
}
@ClearEnvironmentVariable(key = "pioneer")
@Test
void givenEnvironmentVariableIsClear_thenItIsNotSet() {
assertThat(System.getenv("pioneer")).isNull();
}
}
@@ -0,0 +1,31 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
// This test can only work well in Java 15 and below
class EnvironmentVariablesSetDirectlyUnitTest {
@BeforeAll
static void beforeAll() throws Exception {
Class<?> classOfMap = System.getenv().getClass();
Field field = classOfMap.getDeclaredField("m");
field.setAccessible(true);
Map<String, String> writeableEnvironmentVariables = (Map<String, String>)field.get(System.getenv());
writeableEnvironmentVariables.put("baeldung", "has set an environment variable");
}
@Test
void givenEnvironmentVariableWasSet_thenCanReadIt() {
assertThat(System.getenv("baeldung")).isEqualTo("has set an environment variable");
}
@Test
void givenEnvironmentVariableSetBySurefire_thenCanReadIt() {
assertThat(System.getenv("SET_BY_SUREFIRE")).isEqualTo("YES");
}
}
@@ -0,0 +1,18 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.jupiter.api.Test;
import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
import static org.assertj.core.api.Assertions.assertThat;
class EnvironmentVariablesSystemLambdaUnitTest {
@Test
void givenSetEnvironmentVariablesInTest_thenCanBeRead() throws Exception {
withEnvironmentVariable("system lambda", "in test")
.execute(() -> {
assertThat(System.getenv("system lambda")).isEqualTo("in test");
});
}
}
@@ -0,0 +1,23 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import static org.assertj.core.api.Assertions.assertThat;
public class EnvironmentVariablesSystemRulesUnitTest {
@Rule
public EnvironmentVariables environmentVariablesRule = new EnvironmentVariables();
@Before
public void before() {
environmentVariablesRule.set("system rules", "works");
}
@Test
public void givenEnvironmentVariable_thenCanReadIt() {
assertThat(System.getenv("system rules")).isEqualTo("works");
}
}
@@ -0,0 +1,18 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.Rule;
import org.junit.Test;
import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule;
import static org.assertj.core.api.Assertions.assertThat;
public class EnvironmentVariablesSystemStubsJUnit4UnitTest {
@Rule
public EnvironmentVariablesRule environmentVariablesRule =
new EnvironmentVariablesRule("system stubs", "initializes variable");
@Test
public void givenEnvironmentSetUpInObject_thenCanReadIt() {
assertThat(System.getenv("system stubs")).isEqualTo("initializes variable");
}
}
@@ -0,0 +1,27 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SystemStubsExtension.class)
class EnvironmentVariablesSystemStubsJUnit5UnitTest {
@SystemStub
private EnvironmentVariables environmentVariables;
@BeforeEach
void beforeEach() {
environmentVariables.set("systemstubs", "creates stub objects");
}
@Test
void givenEnvironmentVariableHasBeenSet_thenCanReadIt() {
assertThat(System.getenv("systemstubs")).isEqualTo("creates stub objects");
}
}
@@ -0,0 +1,28 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import static org.assertj.core.api.Assertions.assertThat;
class EnvironmentVariablesSystemStubsNoFrameworkClassScopeEnvironmentUnitTest {
private static EnvironmentVariables environmentVariables = new EnvironmentVariables();
@BeforeAll
static void beforeAll() throws Exception {
environmentVariables.set("system stubs", "in test");
environmentVariables.setup();
}
@AfterAll
static void afterAll() throws Exception {
environmentVariables.teardown();
}
@Test
void givenSetEnvironmentVariablesBeforeAll_thenCanBeRead() throws Exception {
assertThat(System.getenv("system stubs")).isEqualTo("in test");
}
}
@@ -0,0 +1,17 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables;
class EnvironmentVariablesSystemStubsNoFrameworkUnitTest {
@Test
void givenSetEnvironmentVariablesInTest_thenCanBeRead() throws Exception {
withEnvironmentVariables("system stubs", "in test")
.execute(() -> {
assertThat(System.getenv("system stubs")).isEqualTo("in test");
});
}
}
@@ -0,0 +1,27 @@
package com.baeldung.environmentvariablesfortest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.testng.SystemStub;
import uk.org.webcompere.systemstubs.testng.SystemStubsListener;
import static org.assertj.core.api.Assertions.assertThat;
@Listeners(SystemStubsListener.class)
public class EnvironmentVariablesSystemStubsTestNGUnitTest {
@SystemStub
private EnvironmentVariables setEnvironment;
@BeforeClass
public void beforeAll() {
setEnvironment.set("testng", "has environment variables");
}
@Test
public void givenEnvironmentVariableWasSet_thenItCanBeRead() {
assertThat(System.getenv("testng"))
.isEqualTo("has environment variables");
}
}