File tree Expand file tree Collapse file tree
testing-modules/testing-libraries
src/test/java/com/baeldung/systemout Expand file tree Collapse file tree Original file line number Diff line number Diff line change 4242 <artifactId >spring-boot-starter-web</artifactId >
4343 <version >2.2.0.RELEASE</version >
4444 </dependency >
45-
45+ <dependency >
46+ <groupId >com.github.stefanbirkner</groupId >
47+ <artifactId >system-rules</artifactId >
48+ <version >${system-rules.version} </version >
49+ <scope >test</scope >
50+ </dependency >
51+ <dependency >
52+ <groupId >com.github.stefanbirkner</groupId >
53+ <artifactId >system-lambda</artifactId >
54+ <version >${system-lambda.version} </version >
55+ <scope >test</scope >
56+ </dependency >
4657 </dependencies >
4758
4859 <build >
90101 <lambda-behave .version>0.4</lambda-behave .version>
91102 <cucumber .version>4.8.0</cucumber .version>
92103 <checkstyle-maven-plugin .version>3.0.0</checkstyle-maven-plugin .version>
104+ <system-rules .version>1.19.0</system-rules .version>
105+ <system-lambda .version>1.0.0</system-lambda .version>
93106 </properties >
94107
95108</project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .systemout ;
2+
3+ import java .io .ByteArrayOutputStream ;
4+ import java .io .PrintStream ;
5+
6+ import org .junit .Assert ;
7+ import org .junit .jupiter .api .BeforeEach ;
8+ import org .junit .jupiter .api .Test ;
9+
10+ import static com .github .stefanbirkner .systemlambda .SystemLambda .tapSystemOut ;
11+
12+ class SystemOutPrintlnUnitTest {
13+
14+ private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream ();
15+ private final PrintStream standardOut = System .out ;
16+
17+ @ BeforeEach
18+ public void setUp () {
19+ System .setOut (new PrintStream (outputStreamCaptor ));
20+ }
21+
22+ @ BeforeEach
23+ public void tearDown () {
24+ System .setOut (standardOut );
25+ }
26+
27+ @ Test
28+ void givenSystemOutRedirection_whenInvokePrintln_thenOutputCaptorSuccess () {
29+ print ("Hello Baeldung Readers!!" );
30+
31+ Assert .assertEquals ("Hello Baeldung Readers!!" , outputStreamCaptor .toString ()
32+ .trim ());
33+ }
34+
35+ @ Test
36+ void givenTapSystemOut_whenInvokePrintln_thenOutputIsReturnedSuccessfully () throws Exception {
37+
38+ String text = tapSystemOut (() -> {
39+ print ("Hello Baeldung Readers!!" );
40+ });
41+
42+ Assert .assertEquals ("Hello Baeldung Readers!!" , text .trim ());
43+ }
44+
45+ private void print (String output ) {
46+ System .out .println (output );
47+ }
48+
49+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .systemout ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .Rule ;
5+ import org .junit .Test ;
6+ import org .junit .contrib .java .lang .system .SystemOutRule ;
7+
8+ public class SystemOutPrintlnWithRuleUnitTest {
9+
10+ @ Rule
11+ public final SystemOutRule systemOutRule = new SystemOutRule ().enableLog ();
12+
13+ @ Test
14+ public void givenSystemOutRule_whenInvokePrintln_thenLogSuccess () {
15+ print ("Hello Baeldung Readers!!" );
16+
17+ Assert .assertEquals ("Hello Baeldung Readers!!" , systemOutRule .getLog ()
18+ .trim ());
19+
20+ Assert .assertEquals ("Hello Baeldung Readers!!\n " , systemOutRule .getLogWithNormalizedLineSeparator ());
21+ }
22+
23+ private void print (String output ) {
24+ System .out .println (output );
25+ }
26+
27+ }
You can’t perform that action at this time.
0 commit comments