这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
32 changes: 31 additions & 1 deletion testing-modules/testing-libraries-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@
<version>${system-stubs.version}</version>
<scope>test</scope>
</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>

<build>
Expand Down Expand Up @@ -127,6 +151,9 @@
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
<environmentVariables>
<SET_BY_SUREFIRE>YES</SET_BY_SUREFIRE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
Expand All @@ -142,7 +169,10 @@
<jacoco.version>0.8.6</jacoco.version>
<system-rules.version>1.19.0</system-rules.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>

</project>
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");
}
}
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();
}
}
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");
}
}
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");
});
}
}
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");
Copy link
Collaborator

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?

Copy link
Contributor Author

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.getenv won't return the value you've just set, because env substitution isn't active. Within the test, the set method is "live"

}

@Test
public void givenEnvironmentVariable_thenCanReadIt() {
assertThat(System.getenv("system rules")).isEqualTo("works");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
});
}
}
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");
}

}