Merge pull request #14817 from ashleyfrieze/BAEL-7006-mock-env-variables

BAEL-7006 Create example code for environment variables article
This commit is contained in:
Liam Williams 2023-10-20 16:10:20 +01:00 committed by GitHub
commit ab85c88147
11 changed files with 280 additions and 1 deletions

View File

@ -53,6 +53,30 @@
<version>${system-stubs.version}</version> <version>${system-stubs.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-testng</artifactId>
<version>${system-stubs.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>${junit.pioneer.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -127,6 +151,9 @@
--add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED
</argLine> </argLine>
<environmentVariables>
<SET_BY_SUREFIRE>YES</SET_BY_SUREFIRE>
</environmentVariables>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
@ -142,7 +169,10 @@
<jacoco.version>0.8.6</jacoco.version> <jacoco.version>0.8.6</jacoco.version>
<system-rules.version>1.19.0</system-rules.version> <system-rules.version>1.19.0</system-rules.version>
<system-lambda.version>1.0.0</system-lambda.version> <system-lambda.version>1.0.0</system-lambda.version>
<system-stubs.version>1.1.0</system-stubs.version> <system-stubs.version>2.1.3</system-stubs.version>
<testng.version>7.8.0</testng.version>
<assertj.version>3.24.2</assertj.version>
<junit.pioneer.version>2.1.0</junit.pioneer.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,37 @@
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");
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.environmentvariablesfortest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import java.lang.reflect.Field;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_16)
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");
}
}

View File

@ -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");
});
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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");
});
}
}

View File

@ -0,0 +1,26 @@
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");
}
}