diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index f1d30cd7a1..0416423239 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -41,7 +41,8 @@ junit-5-advanced xmlunit-2 junit-4 - testing-libraries + testing-libraries + testing-libraries-2 powermock diff --git a/testing-modules/testing-libraries-2/pom.xml b/testing-modules/testing-libraries-2/pom.xml new file mode 100644 index 0000000000..282583c882 --- /dev/null +++ b/testing-modules/testing-libraries-2/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + testing-libraries-2 + testing-libraries-2 + + + com.baeldung + testing-modules + 1.0.0-SNAPSHOT + + + + + com.github.stefanbirkner + system-rules + ${system-rules.version} + test + + + com.github.stefanbirkner + system-lambda + ${system-lambda.version} + test + + + + + testing-libraries + + + src/test/resources + true + + + + + + 1.19.0 + 1.0.0 + + diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ClearSystemPropertiesWithRuleUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ClearSystemPropertiesWithRuleUnitTest.java new file mode 100644 index 0000000000..699b40bad9 --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ClearSystemPropertiesWithRuleUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.systemrules; + +import static org.junit.Assert.assertNull; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.ClearSystemProperties; + +public class ClearSystemPropertiesWithRuleUnitTest { + + @Rule + public final ClearSystemProperties userNameIsClearedRule = new ClearSystemProperties("user.name"); + + @Test + public void givenClearUsernameProperty_whenGetUserName_thenNull() { + assertNull(System.getProperty("user.name")); + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ProvidesSystemPropertyUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ProvidesSystemPropertyUnitTest.java new file mode 100644 index 0000000000..361a338508 --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ProvidesSystemPropertyUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.systemrules; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.restoreSystemProperties; +import static org.junit.Assert.assertEquals; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ProvidesSystemPropertyUnitTest { + + @BeforeAll + static void setUpBeforeClass() throws Exception { + System.setProperty("log_dir", "/tmp/baeldung/logs"); + } + + @Test + void givenSetSystemProperty_whenGetLogDir_thenLogDirIsProvidedSuccessfully() throws Exception { + restoreSystemProperties(() -> { + System.setProperty("log_dir", "test/resources"); + assertEquals("log_dir should be provided", "test/resources", System.getProperty("log_dir")); + }); + + assertEquals("log_dir should be provided", "/tmp/baeldung/logs", System.getProperty("log_dir")); + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ProvidesSystemPropertyWithRuleUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ProvidesSystemPropertyWithRuleUnitTest.java new file mode 100644 index 0000000000..3a90592a0d --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/ProvidesSystemPropertyWithRuleUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.systemrules; + +import static org.junit.Assert.assertEquals; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.ProvideSystemProperty; + +public class ProvidesSystemPropertyWithRuleUnitTest { + + @Rule + public final ProvideSystemProperty providesSystemPropertyRule = new ProvideSystemProperty("log_dir", "test/resources").and("another_property", "another_value"); + + @Rule + public final ProvideSystemProperty providesSystemPropertyFromFileRule = ProvideSystemProperty.fromResource("/test.properties"); + + @BeforeClass + public static void setUpBeforeClass() { + setLogs(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println(System.getProperty("log_dir")); + } + + @Test + public void givenProvideSystemProperty_whenGetLogDir_thenLogDirIsProvidedSuccessfully() { + assertEquals("log_dir should be provided", "test/resources", System.getProperty("log_dir")); + } + + @Test + public void givenProvideSystemPropertyFromFile_whenGetName_thenNameIsProvidedSuccessfully() { + assertEquals("name should be provided", "baeldung", System.getProperty("name")); + assertEquals("version should be provided", "1.0", System.getProperty("version")); + } + + private static void setLogs() { + System.setProperty("log_dir", "/tmp/baeldung/logs"); + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemErrPrintlnUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemErrPrintlnUnitTest.java new file mode 100644 index 0000000000..3345ddae8a --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemErrPrintlnUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.systemrules; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErr; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +class SystemErrPrintlnUnitTest { + + @Test + void givenTapSystemErr_whenInvokePrintln_thenOutputIsReturnedSuccessfully() throws Exception { + + String text = tapSystemErr(() -> { + printError("An error occurred Baeldung Readers!!"); + }); + + Assert.assertEquals("An error occurred Baeldung Readers!!", text.trim()); + } + + private void printError(String output) { + System.err.println(output); + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemErrPrintlnWithRuleUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemErrPrintlnWithRuleUnitTest.java new file mode 100644 index 0000000000..7994f91063 --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemErrPrintlnWithRuleUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.systemrules; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemErrRule; + +public class SystemErrPrintlnWithRuleUnitTest { + + @Rule + public final SystemErrRule systemErrRule = new SystemErrRule().enableLog(); + + @Test + public void givenSystemErrRule_whenInvokePrintln_thenLogSuccess() { + printError("An Error occurred Baeldung Readers!!"); + + Assert.assertEquals("An Error occurred Baeldung Readers!!", systemErrRule.getLog() + .trim()); + } + + private void printError(String output) { + System.err.println(output); + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemExitUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemExitUnitTest.java new file mode 100644 index 0000000000..1e2548e714 --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemExitUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.systemrules; + +import static org.junit.Assert.assertEquals; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.catchSystemExit; + +import org.junit.jupiter.api.Test; + +class SystemExitUnitTest { + + @Test + void givenCatchSystemExit_whenAppCallsSystemExit_thenStatusIsReturnedSuccessfully() throws Exception { + int statusCode = catchSystemExit(() -> { + exit(); + }); + assertEquals("status code should be 1:", 1, statusCode); + } + + private void exit() { + System.exit(1); + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemExitWithRuleUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemExitWithRuleUnitTest.java new file mode 100644 index 0000000000..471aab1989 --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemExitWithRuleUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.systemrules; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.ExpectedSystemExit; + +public class SystemExitWithRuleUnitTest { + + @Rule + public final ExpectedSystemExit exitRule = ExpectedSystemExit.none(); + + @Test + public void givenSystemExitRule_whenAppCallsSystemExit_thenExitRuleWorkssAsExpected() { + exitRule.expectSystemExitWithStatus(1); + exit(); + } + + private void exit() { + System.exit(1); + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemInUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemInUnitTest.java new file mode 100644 index 0000000000..96badb59d4 --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemInUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.systemrules; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.withTextFromSystemIn; +import static org.junit.Assert.assertEquals; + +import java.util.Scanner; + +import org.junit.jupiter.api.Test; + +class SystemInUnitTest { + + @Test + void givenTwoNames_whenSystemInMock_thenNamesJoinedTogether() throws Exception { + withTextFromSystemIn("Jonathan", "Cook").execute(() -> { + assertEquals("Names should be concatenated", "Jonathan Cook", getFullname()); + }); + } + + private String getFullname() { + try (Scanner scanner = new Scanner(System.in)) { + String firstName = scanner.next(); + String surname = scanner.next(); + return String.join(" ", firstName, surname); + } + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemInWithRuleUnitTest.java b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemInWithRuleUnitTest.java new file mode 100644 index 0000000000..5730e2f592 --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemrules/SystemInWithRuleUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.systemrules; + +import static org.junit.Assert.assertEquals; + +import java.util.Scanner; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.TextFromStandardInputStream; +import static org.junit.contrib.java.lang.system.TextFromStandardInputStream.emptyStandardInputStream; + +public class SystemInWithRuleUnitTest { + + @Rule + public final TextFromStandardInputStream systemInMock = emptyStandardInputStream(); + + @Test + public void givenTwoNames_whenSystemInMock_thenNamesJoinedTogether() { + systemInMock.provideLines("Jonathan", "Cook"); + assertEquals("Names should be concatenated", "Jonathan Cook", getFullname()); + } + + private String getFullname() { + try (Scanner scanner = new Scanner(System.in)) { + String firstName = scanner.next(); + String surname = scanner.next(); + return String.join(" ", firstName, surname); + } + } + +} diff --git a/testing-modules/testing-libraries-2/src/test/resources/test.properties b/testing-modules/testing-libraries-2/src/test/resources/test.properties new file mode 100644 index 0000000000..144847cdb0 --- /dev/null +++ b/testing-modules/testing-libraries-2/src/test/resources/test.properties @@ -0,0 +1,2 @@ +name=baeldung +version=1.0 \ No newline at end of file