-
Notifications
You must be signed in to change notification settings - Fork 54k
BAEL-7006 Create example code for environment variables article #14817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
theangrydev
merged 2 commits into
eugenp:master
from
ashleyfrieze:BAEL-7006-mock-env-variables
Oct 20, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...a/com/baeldung/environmentvariablesfortest/EnvironmentVariablesByAbstractionUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...m/baeldung/environmentvariablesfortest/EnvironmentVariablesSetByJUnitPioneerUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
...ava/com/baeldung/environmentvariablesfortest/EnvironmentVariablesSetDirectlyUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
...va/com/baeldung/environmentvariablesfortest/EnvironmentVariablesSystemLambdaUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| }); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...ava/com/baeldung/environmentvariablesfortest/EnvironmentVariablesSystemRulesUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
...m/baeldung/environmentvariablesfortest/EnvironmentVariablesSystemStubsJUnit4UnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...m/baeldung/environmentvariablesfortest/EnvironmentVariablesSystemStubsJUnit5UnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...ablesfortest/EnvironmentVariablesSystemStubsNoFrameworkClassScopeEnvironmentUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
...ldung/environmentvariablesfortest/EnvironmentVariablesSystemStubsNoFrameworkUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| }); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
...m/baeldung/environmentvariablesfortest/EnvironmentVariablesSystemStubsTestNGUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, does this work equally well if the variable is set in the test itself, or does it have to be in a
@Before?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can set an environment variable at any point. During before,
System.getenvwon't return the value you've just set, because env substitution isn't active. Within the test, thesetmethod is "live"