BAEL-4341 - JUnit test for System.out.println()
This commit is contained in:
parent
df6546ff28
commit
23dbbb579d
|
@ -42,7 +42,18 @@
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
<version>2.2.0.RELEASE</version>
|
<version>2.2.0.RELEASE</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -90,6 +101,8 @@
|
||||||
<lambda-behave.version>0.4</lambda-behave.version>
|
<lambda-behave.version>0.4</lambda-behave.version>
|
||||||
<cucumber.version>4.8.0</cucumber.version>
|
<cucumber.version>4.8.0</cucumber.version>
|
||||||
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.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>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue