diff --git a/testing-modules/testing-libraries-2/pom.xml b/testing-modules/testing-libraries-2/pom.xml
index 282583c882..42c84d0da9 100644
--- a/testing-modules/testing-libraries-2/pom.xml
+++ b/testing-modules/testing-libraries-2/pom.xml
@@ -9,9 +9,16 @@
com.baeldung
testing-modules
1.0.0-SNAPSHOT
+ ../
+
+ org.assertj
+ assertj-core
+ 3.16.1
+ test
+
com.github.stefanbirkner
system-rules
@@ -24,6 +31,44 @@
${system-lambda.version}
test
+
+ uk.org.webcompere
+ system-stubs-jupiter
+ ${system-stubs.version}
+ test
+
+
+ uk.org.webcompere
+ system-stubs-junit4
+ ${system-stubs.version}
+ test
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ ${junit.jupiter.version}
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
+ ${junit.jupiter.version}
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-params
+ ${junit.jupiter.version}
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ ${junit.jupiter.version}
+ test
+
@@ -39,5 +84,7 @@
1.19.0
1.0.0
+ 1.1.0
+ 5.6.2
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseJUnit5UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseJUnit5UnitTest.java
new file mode 100644
index 0000000000..cb9371bd69
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseJUnit5UnitTest.java
@@ -0,0 +1,16 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@ExtendWith(SystemStubsExtension.class)
+class FakeDatabaseJUnit5UnitTest {
+
+ @Test
+ void useFakeDatabase(FakeDatabaseTestResource fakeDatabase) {
+ assertThat(fakeDatabase.getDatabaseConnection()).isEqualTo("open");
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseTestResource.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseTestResource.java
new file mode 100644
index 0000000000..6cb1b1d607
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseTestResource.java
@@ -0,0 +1,22 @@
+package com.baeldung.systemstubs;
+
+import uk.org.webcompere.systemstubs.resource.TestResource;
+
+public class FakeDatabaseTestResource implements TestResource {
+ // let's pretend this is a database connection
+ private String databaseConnection = "closed";
+
+ @Override
+ public void setup() throws Exception {
+ databaseConnection = "open";
+ }
+
+ @Override
+ public void teardown() throws Exception {
+ databaseConnection = "closed";
+ }
+
+ public String getDatabaseConnection() {
+ return databaseConnection;
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseTestResourceUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseTestResourceUnitTest.java
new file mode 100644
index 0000000000..6b8fdcecdd
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/FakeDatabaseTestResourceUnitTest.java
@@ -0,0 +1,21 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class FakeDatabaseTestResourceUnitTest {
+ @Nested
+ class ExecuteAround {
+ @Test
+ void theResourceIsClosedToStartWith() throws Exception {
+ FakeDatabaseTestResource fake = new FakeDatabaseTestResource();
+ assertThat(fake.getDatabaseConnection()).isEqualTo("closed");
+
+ fake.execute(() -> {
+ assertThat(fake.getDatabaseConnection()).isEqualTo("open");
+ });
+ }
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/GettingStartedWithSystemStubsJUnit4UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/GettingStartedWithSystemStubsJUnit4UnitTest.java
new file mode 100644
index 0000000000..6eaffac7ed
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/GettingStartedWithSystemStubsJUnit4UnitTest.java
@@ -0,0 +1,58 @@
+package com.baeldung.systemstubs;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@RunWith(Enclosed.class)
+public class GettingStartedWithSystemStubsJUnit4UnitTest {
+ public static class SetEnvironmentInsideTest {
+ @Rule
+ public EnvironmentVariablesRule environmentVariablesRule = new EnvironmentVariablesRule();
+
+ @Test
+ public void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
+ environmentVariablesRule.set("ENV", "value1");
+
+ assertThat(System.getenv("ENV")).isEqualTo("value1");
+ }
+ }
+
+ public static class SetEnvironmentAtConstruction {
+ @Rule
+ public EnvironmentVariablesRule environmentVariablesRule =
+ new EnvironmentVariablesRule("ENV", "value1",
+ "ENV2", "value2");
+
+
+ @Test
+ public void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
+ assertThat(System.getenv("ENV")).isEqualTo("value1");
+ assertThat(System.getenv("ENV2")).isEqualTo("value2");
+ }
+ }
+
+ public static class SetEnvironmentInBefore {
+ @Rule
+ public EnvironmentVariablesRule environmentVariablesRule =
+ new EnvironmentVariablesRule();
+
+ @Before
+ public void before() {
+ environmentVariablesRule.set("ENV", "value1")
+ .set("ENV2", "value2");
+ }
+
+
+ @Test
+ public void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
+ assertThat(System.getenv("ENV")).isEqualTo("value1");
+ assertThat(System.getenv("ENV2")).isEqualTo("value2");
+ }
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/GettingStartedWithSystemStubsUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/GettingStartedWithSystemStubsUnitTest.java
new file mode 100644
index 0000000000..76fb768d34
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/GettingStartedWithSystemStubsUnitTest.java
@@ -0,0 +1,109 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Nested;
+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 uk.org.webcompere.systemstubs.properties.SystemProperties;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariable;
+import static uk.org.webcompere.systemstubs.resource.Resources.with;
+
+class GettingStartedWithSystemStubsUnitTest {
+ @Nested
+ @ExtendWith(SystemStubsExtension.class)
+ class EnvironmentVariablesJUnit5 {
+ @SystemStub
+ private EnvironmentVariables environmentVariables;
+
+ @Test
+ void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
+ environmentVariables.set("ENV", "value1");
+
+ assertThat(System.getenv("ENV")).isEqualTo("value1");
+ }
+ }
+
+ @Nested
+ @ExtendWith(SystemStubsExtension.class)
+ class EnvironmentVariablesConstructedJUnit5 {
+ @SystemStub
+ private EnvironmentVariables environmentVariables =
+ new EnvironmentVariables("ENV", "value1");
+
+ @Test
+ void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
+ assertThat(System.getenv("ENV")).isEqualTo("value1");
+ }
+ }
+
+ @Nested
+ @ExtendWith(SystemStubsExtension.class)
+ class EnvironmentVariablesConstructedWithSetJUnit5 {
+ @SystemStub
+ private EnvironmentVariables environmentVariables =
+ new EnvironmentVariables()
+ .set("ENV", "value1")
+ .set("ENV2", "value2");
+
+ @Test
+ void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
+ assertThat(System.getenv("ENV")).isEqualTo("value1");
+ }
+ }
+
+ @Nested
+ @ExtendWith(SystemStubsExtension.class)
+ class EnvironmentVariablesJUnit5ParameterInjection {
+ @Test
+ void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet(EnvironmentVariables environmentVariables) {
+ environmentVariables.set("ENV", "value1");
+
+ assertThat(System.getenv("ENV")).isEqualTo("value1");
+ }
+ }
+
+ @Nested
+ class EnvironmentVariablesExecuteAround {
+ @Test
+ void givenSetupUsingWithEnvironmentVariable_thenItIsSet() throws Exception {
+ withEnvironmentVariable("ENV3", "val")
+ .execute(() -> {
+ assertThat(System.getenv("ENV3")).isEqualTo("val");
+ });
+ }
+
+ @Test
+ void givenSetupUsingConstructor_thenItIsSet() throws Exception {
+ EnvironmentVariables environment = new EnvironmentVariables()
+ .set("ENV3", "val");
+ environment.execute(() -> {
+ assertThat(System.getenv("ENV3")).isEqualTo("val");
+ });
+ }
+
+ @Test
+ void givenEnvironment_thenCanReturnValue() throws Exception {
+ String extracted = new EnvironmentVariables("PROXY", "none")
+ .execute(() -> System.getenv("PROXY"));
+
+ assertThat(extracted).isEqualTo("none");
+ }
+ }
+
+ @Nested
+ class RunMultiple {
+ @Test
+ void runMultiple() throws Exception {
+ with(new EnvironmentVariables("FOO", "bar"),
+ new SystemProperties("prop", "val"))
+ .execute(() -> {
+ assertThat(System.getenv("FOO")).isEqualTo("bar");
+ assertThat(System.getProperty("prop")).isEqualTo("val");
+ });
+ }
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/JUnit4SystemPropertiesUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/JUnit4SystemPropertiesUnitTest.java
new file mode 100644
index 0000000000..111baf9e9c
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/JUnit4SystemPropertiesUnitTest.java
@@ -0,0 +1,42 @@
+package com.baeldung.systemstubs;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import uk.org.webcompere.systemstubs.rules.SystemPropertiesRule;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class JUnit4SystemPropertiesUnitTest {
+ @Rule
+ public SystemPropertiesRule systemProperties =
+ new SystemPropertiesRule("db.connection", "false");
+
+ @Before
+ public void before() {
+ systemProperties.set("before.prop", "before");
+ }
+
+ @Test
+ public void givenPropertyIsSet_thenCanBeUsedInTest() {
+ assertThat(System.getProperty("db.connection")).isEqualTo("false");
+ }
+
+ @Test
+ public void givenPropertyIsSet_thenAnotherCanBeSetAndBeUsedInTest() {
+ assertThat(System.getProperty("db.connection")).isEqualTo("false");
+
+ systemProperties.set("prop2", "true");
+ assertThat(System.getProperty("prop2")).isEqualTo("true");
+ }
+
+ @Test
+ public void givenPropertySetInBefore_thenCanBeSeenInTest() {
+ assertThat(System.getProperty("before.prop")).isEqualTo("before");
+ }
+
+ @Test
+ public void givenPropertySetEarlier_thenNotVisibleLater() {
+ assertThat(System.getProperty("prop2")).isNull();
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/JUnit5SystemPropertiesUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/JUnit5SystemPropertiesUnitTest.java
new file mode 100644
index 0000000000..7aaf4cebad
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/JUnit5SystemPropertiesUnitTest.java
@@ -0,0 +1,89 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import uk.org.webcompere.systemstubs.jupiter.SystemStub;
+import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
+import uk.org.webcompere.systemstubs.properties.SystemProperties;
+import uk.org.webcompere.systemstubs.resource.PropertySource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class JUnit5SystemPropertiesUnitTest {
+
+ @ExtendWith(SystemStubsExtension.class)
+ @Nested
+ class RestoreSystemProperties {
+ @SystemStub
+ private SystemProperties systemProperties;
+
+ @Test
+ void givenAPropertyIsSet_thenItIsOnlyAvailableInsideThisTest1() {
+ assertThat(System.getProperty("localProperty")).isNull();
+
+ System.setProperty("localProperty", "nonnull");
+ assertThat(System.getProperty("localProperty")).isEqualTo("nonnull");
+ }
+
+ @Test
+ void givenAPropertyIsSet_thenItIsOnlyAvailableInsideThisTest2() {
+ assertThat(System.getProperty("localProperty")).isNull();
+
+ System.setProperty("localProperty", "true");
+ assertThat(System.getProperty("localProperty")).isEqualTo("true");
+ }
+ }
+
+ @ExtendWith(SystemStubsExtension.class)
+ @Nested
+ class RestoreSystemPropertiesByParameter {
+
+ @Test
+ void givenAPropertyIsSet_thenItIsOnlyAvailableInsideThisTest1(SystemProperties systemProperties) {
+ assertThat(System.getProperty("localProperty")).isNull();
+
+ System.setProperty("localProperty", "nonnull");
+ assertThat(System.getProperty("localProperty")).isEqualTo("nonnull");
+ }
+
+ @Test
+ void givenAPropertyIsSet_thenItIsOnlyAvailableInsideThisTest2(SystemProperties systemProperties) {
+ assertThat(System.getProperty("localProperty")).isNull();
+
+ System.setProperty("localProperty", "true");
+ assertThat(System.getProperty("localProperty")).isEqualTo("true");
+ }
+ }
+
+ @ExtendWith(SystemStubsExtension.class)
+ @Nested
+ class SetSomeSystemProperties {
+ @SystemStub
+ private SystemProperties systemProperties;
+
+ @BeforeEach
+ void before() {
+ systemProperties.set("beforeProperty", "before");
+ }
+
+ @Test
+ void givenAPropertyIsSetInBefore_thenItIsAvailableInsideThisTest() {
+ assertThat(System.getProperty("beforeProperty")).isEqualTo("before");
+ }
+ }
+
+ @ExtendWith(SystemStubsExtension.class)
+ @Nested
+ class SetSomeSystemPropertiesFromResources {
+ @SystemStub
+ private SystemProperties systemProperties =
+ new SystemProperties(PropertySource.fromResource("test.properties"));
+
+ @Test
+ void givenPropertiesReadFromResources_thenCanBeUsed() {
+ assertThat(System.getProperty("name")).isEqualTo("baeldung");
+ }
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/OutputMutingJUnit4UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/OutputMutingJUnit4UnitTest.java
new file mode 100644
index 0000000000..a178efbb9d
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/OutputMutingJUnit4UnitTest.java
@@ -0,0 +1,16 @@
+package com.baeldung.systemstubs;
+
+import org.junit.Rule;
+import org.junit.Test;
+import uk.org.webcompere.systemstubs.rules.SystemOutRule;
+import uk.org.webcompere.systemstubs.stream.output.NoopStream;
+
+public class OutputMutingJUnit4UnitTest {
+ @Rule
+ public SystemOutRule systemOutRule = new SystemOutRule(new NoopStream());
+
+ @Test
+ public void givenMuteSystemOut() throws Exception {
+ System.out.println("nothing is output");
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/OutputMutingUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/OutputMutingUnitTest.java
new file mode 100644
index 0000000000..c75a523fff
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/OutputMutingUnitTest.java
@@ -0,0 +1,35 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import uk.org.webcompere.systemstubs.jupiter.SystemStub;
+import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
+import uk.org.webcompere.systemstubs.stream.SystemOut;
+import uk.org.webcompere.systemstubs.stream.output.NoopStream;
+
+import static uk.org.webcompere.systemstubs.SystemStubs.muteSystemOut;
+
+class OutputMutingUnitTest {
+ @Nested
+ class MutingWithFacade {
+ @Test
+ void givenMuteSystemOut() throws Exception {
+ muteSystemOut(() -> {
+ System.out.println("nothing is output");
+ });
+ }
+ }
+
+ @ExtendWith(SystemStubsExtension.class)
+ @Nested
+ class MutingWithJUnit5 {
+ @SystemStub
+ private SystemOut systemOut = new SystemOut(new NoopStream());
+
+ @Test
+ void givenMuteSystemOut() throws Exception {
+ System.out.println("nothing is output");
+ }
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitExecuteAroundUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitExecuteAroundUnitTest.java
new file mode 100644
index 0000000000..b42cc43307
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitExecuteAroundUnitTest.java
@@ -0,0 +1,17 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static uk.org.webcompere.systemstubs.SystemStubs.catchSystemExit;
+
+class SystemExitExecuteAroundUnitTest {
+ @Test
+ void canCheckExitCode() throws Exception {
+ int exitCode = catchSystemExit(() -> {
+ System.exit(123);
+ });
+ assertThat(exitCode).isEqualTo(123);
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitJUnit4UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitJUnit4UnitTest.java
new file mode 100644
index 0000000000..c044e250dd
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitJUnit4UnitTest.java
@@ -0,0 +1,29 @@
+package com.baeldung.systemstubs;
+
+import org.junit.Rule;
+import org.junit.Test;
+import uk.org.webcompere.systemstubs.rules.SystemExitRule;
+import uk.org.webcompere.systemstubs.security.AbortExecutionException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class SystemExitJUnit4UnitTest {
+ @Rule
+ public SystemExitRule systemExitRule = new SystemExitRule();
+
+ @Test
+ public void whenAccidentalSystemExit_thenTestFailsRatherThanJVMKilled() {
+ // uncomment this to try it
+ //System.exit(1);
+ }
+
+ @Test
+ public void whenExit_thenExitCodeIsAvailable() {
+ assertThatThrownBy(() -> {
+ System.exit(123);
+ }).isInstanceOf(AbortExecutionException.class);
+
+ assertThat(systemExitRule.getExitCode()).isEqualTo(123);
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitJUnit5UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitJUnit5UnitTest.java
new file mode 100644
index 0000000000..4451d7e31f
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemExitJUnit5UnitTest.java
@@ -0,0 +1,26 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import uk.org.webcompere.systemstubs.jupiter.SystemStub;
+import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
+import uk.org.webcompere.systemstubs.security.AbortExecutionException;
+import uk.org.webcompere.systemstubs.security.SystemExit;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+@ExtendWith(SystemStubsExtension.class)
+class SystemExitJUnit5UnitTest {
+ @SystemStub
+ private SystemExit systemExit;
+
+ @Test
+ void whenExit_thenExitCodeIsAvailable() {
+ assertThatThrownBy(() -> {
+ System.exit(123);
+ }).isInstanceOf(AbortExecutionException.class);
+
+ assertThat(systemExit.getExitCode()).isEqualTo(123);
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInExecuteAroundUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInExecuteAroundUnitTest.java
new file mode 100644
index 0000000000..5a1d510e35
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInExecuteAroundUnitTest.java
@@ -0,0 +1,20 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Scanner;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static uk.org.webcompere.systemstubs.SystemStubs.withTextFromSystemIn;
+
+class SystemInExecuteAroundUnitTest {
+
+ @Test
+ void givenTextInSystemIn_thenCanReadIt() throws Exception {
+ withTextFromSystemIn("line1", "line2", "line3")
+ .execute(() -> {
+ assertThat(new Scanner(System.in).nextLine())
+ .isEqualTo("line1");
+ });
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInJUnit4UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInJUnit4UnitTest.java
new file mode 100644
index 0000000000..ccd422ea24
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInJUnit4UnitTest.java
@@ -0,0 +1,21 @@
+package com.baeldung.systemstubs;
+
+import org.junit.Rule;
+import org.junit.Test;
+import uk.org.webcompere.systemstubs.rules.SystemInRule;
+
+import java.util.Scanner;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SystemInJUnit4UnitTest {
+ @Rule
+ public SystemInRule systemInRule =
+ new SystemInRule("line1", "line2", "line3");
+
+ @Test
+ public void givenInput_canReadFirstLine() {
+ assertThat(new Scanner(System.in).nextLine())
+ .isEqualTo("line1");
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInJUnit5UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInJUnit5UnitTest.java
new file mode 100644
index 0000000000..ed506eb40e
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemInJUnit5UnitTest.java
@@ -0,0 +1,23 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import uk.org.webcompere.systemstubs.jupiter.SystemStub;
+import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
+import uk.org.webcompere.systemstubs.stream.SystemIn;
+
+import java.util.Scanner;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@ExtendWith(SystemStubsExtension.class)
+class SystemInJUnit5UnitTest {
+ @SystemStub
+ private SystemIn systemIn = new SystemIn("line1", "line2", "line3");
+
+ @Test
+ void givenInput_canReadFirstLine() {
+ assertThat(new Scanner(System.in).nextLine())
+ .isEqualTo("line1");
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemLambdaComparisonUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemLambdaComparisonUnitTest.java
new file mode 100644
index 0000000000..23e65767a8
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemLambdaComparisonUnitTest.java
@@ -0,0 +1,49 @@
+package com.baeldung.systemstubs;
+
+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 uk.org.webcompere.systemstubs.properties.SystemProperties;
+
+import static com.github.stefanbirkner.systemlambda.SystemLambda.restoreSystemProperties;
+import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
+import static org.junit.Assert.assertEquals;
+
+@ExtendWith(SystemStubsExtension.class)
+class SystemLambdaComparisonUnitTest {
+ @SystemStub
+ private EnvironmentVariables environmentVariables =
+ new EnvironmentVariables("ADDRESS", "https://www.baeldung.com");
+
+ @SystemStub
+ private SystemProperties systemProperties = new SystemProperties();
+
+ @Test
+ void aSingleSystemLambda() throws Exception {
+ restoreSystemProperties(() -> {
+ System.setProperty("log_dir", "test/resources");
+ assertEquals("test/resources", System.getProperty("log_dir"));
+ });
+ }
+
+ @Test
+ void multipleSystemLambdas() throws Exception {
+ restoreSystemProperties(() -> {
+ withEnvironmentVariable("URL", "https://www.baeldung.com")
+ .execute(() -> {
+ System.setProperty("log_dir", "test/resources");
+ assertEquals("test/resources", System.getProperty("log_dir"));
+ assertEquals("https://www.baeldung.com", System.getenv("URL"));
+ });
+ });
+ }
+
+ @Test
+ void multipleSystemStubs() {
+ System.setProperty("log_dir", "test/resources");
+ assertEquals("test/resources", System.getProperty("log_dir"));
+ assertEquals("https://www.baeldung.com", System.getenv("ADDRESS"));
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutAndErrExecuteAroundUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutAndErrExecuteAroundUnitTest.java
new file mode 100644
index 0000000000..36b127d3cb
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutAndErrExecuteAroundUnitTest.java
@@ -0,0 +1,43 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+import uk.org.webcompere.systemstubs.properties.SystemProperties;
+import uk.org.webcompere.systemstubs.stream.SystemOut;
+import uk.org.webcompere.systemstubs.stream.output.DisallowWriteStream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static uk.org.webcompere.systemstubs.SystemStubs.tapSystemOutNormalized;
+import static uk.org.webcompere.systemstubs.resource.Resources.with;
+
+class SystemOutAndErrExecuteAroundUnitTest {
+ @Test
+ void givenTapOutput_thenGetOutput() throws Exception {
+ String output = tapSystemOutNormalized(() -> {
+ System.out.println("a");
+ System.out.println("b");
+ });
+
+ assertThat(output).isEqualTo("a\nb\n");
+ }
+
+ @Test
+ void givenCaptureOutputWithSystemOut_thenGetOutput() throws Exception {
+ SystemOut systemOut = new SystemOut();
+ SystemProperties systemProperties = new SystemProperties("a", "!");
+ with(systemOut, systemProperties)
+ .execute(() -> {
+ System.out.println("a: " + System.getProperty("a"));
+ });
+
+ assertThat(systemOut.getLines()).containsExactly("a: !");
+ }
+
+ @Test
+ void givenCannotWrite_thenWritingIsError() {
+ assertThatThrownBy(() -> {
+ new SystemOut(new DisallowWriteStream())
+ .execute(() -> System.out.println("boo"));
+ }).isInstanceOf(AssertionError.class);
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutJUnit4UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutJUnit4UnitTest.java
new file mode 100644
index 0000000000..d6ff70a49b
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutJUnit4UnitTest.java
@@ -0,0 +1,52 @@
+package com.baeldung.systemstubs;
+
+import org.junit.Rule;
+import org.junit.Test;
+import uk.org.webcompere.systemstubs.rules.SystemErrRule;
+import uk.org.webcompere.systemstubs.rules.SystemOutRule;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SystemOutJUnit4UnitTest {
+ @Rule
+ public SystemOutRule systemOutRule = new SystemOutRule();
+
+ @Rule
+ public SystemErrRule systemErrRule = new SystemErrRule();
+
+ @Test
+ public void whenCodeWritesToSystemOut_itCanBeRead() {
+ System.out.println("line1");
+ System.out.println("line2");
+
+ assertThat(systemOutRule.getLines())
+ .containsExactly("line1", "line2");
+ }
+
+ @Test
+ public void whenCodeWritesToSystemOut_itCanBeReadAsText() {
+ System.out.println("line1");
+ System.out.println("line2");
+
+ assertThat(systemOutRule.getText())
+ .startsWith("line1");
+ }
+
+ @Test
+ public void whenCodeWritesToSystemOut_itCanBeReadAsNormalizedLines() {
+ System.out.println("line1");
+ System.out.println("line2");
+
+ assertThat(systemOutRule.getLinesNormalized())
+ .isEqualTo("line1\nline2\n");
+ }
+
+ @Test
+ public void whenCodeWritesToSystemErr_itCanBeRead() {
+ System.err.println("line1");
+ System.err.println("line2");
+
+ assertThat(systemErrRule.getLines())
+ .containsExactly("line1", "line2");
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutJUnit5UnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutJUnit5UnitTest.java
new file mode 100644
index 0000000000..caa8a9264d
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemOutJUnit5UnitTest.java
@@ -0,0 +1,28 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import uk.org.webcompere.systemstubs.jupiter.SystemStub;
+import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
+import uk.org.webcompere.systemstubs.stream.SystemErr;
+import uk.org.webcompere.systemstubs.stream.SystemOut;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@ExtendWith(SystemStubsExtension.class)
+class SystemOutJUnit5UnitTest {
+ @SystemStub
+ private SystemOut systemOut;
+
+ @SystemStub
+ private SystemErr systemErr;
+
+ @Test
+ void whenWriteToOutput_thenItCanBeAsserted() {
+ System.out.println("to out");
+ System.err.println("to err");
+
+ assertThat(systemOut.getLines()).containsExactly("to out");
+ assertThat(systemErr.getLines()).containsExactly("to err");
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemPropertiesExecuteAroundUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemPropertiesExecuteAroundUnitTest.java
new file mode 100644
index 0000000000..3f0f780238
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemPropertiesExecuteAroundUnitTest.java
@@ -0,0 +1,31 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+import uk.org.webcompere.systemstubs.properties.SystemProperties;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static uk.org.webcompere.systemstubs.SystemStubs.restoreSystemProperties;
+
+class SystemPropertiesExecuteAroundUnitTest {
+ @Test
+ void givenRestoreSystemProperties_thenPropertyRestored() throws Exception {
+ restoreSystemProperties(() -> {
+ // test code
+ System.setProperty("unrestored", "true");
+ });
+
+ assertThat(System.getProperty("unrestored")).isNull();
+ }
+
+ @Test
+ void givenSystemPropertiesObject_thenPropertyRestored() throws Exception {
+ String result = new SystemProperties()
+ .execute(() -> {
+ System.setProperty("unrestored", "true");
+ return "it works";
+ });
+
+ assertThat(result).isEqualTo("it works");
+ assertThat(System.getProperty("unrestored")).isNull();
+ }
+}
diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/WithMockedInputStreamUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/WithMockedInputStreamUnitTest.java
new file mode 100644
index 0000000000..1019781837
--- /dev/null
+++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/WithMockedInputStreamUnitTest.java
@@ -0,0 +1,18 @@
+package com.baeldung.systemstubs;
+
+import org.junit.jupiter.api.Test;
+import uk.org.webcompere.systemstubs.stream.input.LinesAltStream;
+
+import java.util.Scanner;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class WithMockedInputStreamUnitTest {
+ @Test
+ void givenInputStream_thenCanRead() {
+ LinesAltStream testInput = new LinesAltStream("line1", "line2");
+
+ Scanner scanner = new Scanner(testInput);
+ assertThat(scanner.nextLine()).isEqualTo("line1");
+ }
+}