BAEL-4341 - JUnit test for System.out.println() (#9694)

* BAEL-4198 - Fix Selenium Live Tests

* BAEL-4198

* BAEL-4024 - Taking Screenshots with Selenium WebDriver

* BAEL-4341 - JUnit test for System.out.println()

Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
Jonathan Cook 2020-07-14 03:46:32 +02:00 committed by GitHub
parent 205c49b3e4
commit a8dac80752
3 changed files with 90 additions and 1 deletions

View File

@ -42,7 +42,18 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>${system-rules.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<version>${system-lambda.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -90,6 +101,8 @@
<lambda-behave.version>0.4</lambda-behave.version>
<cucumber.version>4.8.0</cucumber.version>
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.version>
<system-rules.version>1.19.0</system-rules.version>
<system-lambda.version>1.0.0</system-lambda.version>
</properties>
</project>

View File

@ -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);
}
}

View File

@ -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);
}
}