From 390b864ff385b25483e80806098bc1ad27b44441 Mon Sep 17 00:00:00 2001 From: Ashley Frieze Date: Fri, 20 Nov 2020 17:44:42 +0000 Subject: [PATCH] BAEL-4742-Guide-To-System-Stubs --- testing-modules/testing-libraries-2/pom.xml | 41 ++++++++++++++++ .../SystemLambdaComparisonUnitTest.java | 49 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemstubs/SystemLambdaComparisonUnitTest.java diff --git a/testing-modules/testing-libraries-2/pom.xml b/testing-modules/testing-libraries-2/pom.xml index 282583c882..dd863af6ee 100644 --- a/testing-modules/testing-libraries-2/pom.xml +++ b/testing-modules/testing-libraries-2/pom.xml @@ -9,6 +9,7 @@ com.baeldung testing-modules 1.0.0-SNAPSHOT + ../ @@ -24,6 +25,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 +78,7 @@ 1.19.0 1.0.0 + 1.0.0 + 5.6.2 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")); + } +}