diff --git a/testing-modules/testing-libraries/pom.xml b/testing-modules/testing-libraries/pom.xml index 53b58cee17..aa22a5253e 100644 --- a/testing-modules/testing-libraries/pom.xml +++ b/testing-modules/testing-libraries/pom.xml @@ -42,7 +42,18 @@ spring-boot-starter-web 2.2.0.RELEASE - + + com.github.stefanbirkner + system-rules + ${system-rules.version} + test + + + com.github.stefanbirkner + system-lambda + ${system-lambda.version} + test + @@ -90,6 +101,8 @@ 0.4 4.8.0 3.0.0 + 1.19.0 + 1.0.0 diff --git a/testing-modules/testing-libraries/src/test/java/com/baeldung/systemout/SystemOutPrintlnUnitTest.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/systemout/SystemOutPrintlnUnitTest.java new file mode 100644 index 0000000000..3ffc508fa5 --- /dev/null +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/systemout/SystemOutPrintlnUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.systemout; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut; + +class SystemOutPrintlnUnitTest { + + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + private final PrintStream standardOut = System.out; + + @BeforeEach + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @BeforeEach + public void tearDown() { + System.setOut(standardOut); + } + + @Test + void givenSystemOutRedirection_whenInvokePrintln_thenOutputCaptorSuccess() { + print("Hello Baeldung Readers!!"); + + Assert.assertEquals("Hello Baeldung Readers!!", outputStreamCaptor.toString() + .trim()); + } + + @Test + void givenTapSystemOut_whenInvokePrintln_thenOutputIsReturnedSuccessfully() throws Exception { + + String text = tapSystemOut(() -> { + print("Hello Baeldung Readers!!"); + }); + + Assert.assertEquals("Hello Baeldung Readers!!", text.trim()); + } + + private void print(String output) { + System.out.println(output); + } + +} diff --git a/testing-modules/testing-libraries/src/test/java/com/baeldung/systemout/SystemOutPrintlnWithRuleUnitTest.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/systemout/SystemOutPrintlnWithRuleUnitTest.java new file mode 100644 index 0000000000..f15b71999e --- /dev/null +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/systemout/SystemOutPrintlnWithRuleUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.systemout; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class SystemOutPrintlnWithRuleUnitTest { + + @Rule + public final SystemOutRule systemOutRule = new SystemOutRule().enableLog(); + + @Test + public void givenSystemOutRule_whenInvokePrintln_thenLogSuccess() { + print("Hello Baeldung Readers!!"); + + Assert.assertEquals("Hello Baeldung Readers!!", systemOutRule.getLog() + .trim()); + + Assert.assertEquals("Hello Baeldung Readers!!\n", systemOutRule.getLogWithNormalizedLineSeparator()); + } + + private void print(String output) { + System.out.println(output); + } + +}