87
87
* applications you need to test the output of these applications. The methods
88
88
* {@link #tapSystemErr(Statement) tapSystemErr},
89
89
* {@link #tapSystemErrNormalized(Statement) tapSystemErrNormalized},
90
- * {@link #tapSystemOut(Statement) tapSystemOut} and
91
- * {@link #tapSystemOutNormalized(Statement) tapSystemOutNormalized} allow you
90
+ * {@link #tapSystemOut(Statement) tapSystemOut},
91
+ * {@link #tapSystemOutNormalized(Statement) tapSystemOutNormalized},
92
+ * {@link #tapSystemErrAndOut(Statement) tapSystemErrAndOut} and
93
+ * {@link #tapSystemErrAndOutNormalized(Statement) tapSystemErrAndOutNormalized}
94
+ * allow you
92
95
* to tap the text that is written to {@code System.err}/{@code System.out}. The
93
96
* methods with the suffix {@code Normalized} normalize line breaks to
94
97
* {@code \n} so that you can run tests with the same assertions on different
131
134
* System.out.println("second line");
132
135
* });
133
136
* assertEquals(text, "first line\nsecond line\n");
137
+ * }
138
+ *
139
+ * @Test
140
+ * void application_writes_text_to_System_err_and_out(
141
+ * ) throws Exception {
142
+ * String text = tapSystemErrAndOut((){@literal ->} {
143
+ * System.err.print("text from err");
144
+ * System.out.print("text from out");
145
+ * });
146
+ * assertEquals("text from errtext from out", text);
147
+ * }
148
+ *
149
+ * @Test
150
+ * void application_writes_mutliple_lines_to_System_err_and_out(
151
+ * ) throws Exception {
152
+ * String text = tapSystemErrAndOutNormalized((){@literal ->} {
153
+ * System.err.println("text from err");
154
+ * System.out.println("text from out");
155
+ * });
156
+ * assertEquals("text from err\ntext from out\n", text);
134
157
* }</pre>
135
158
*
136
159
* <p>You can assert that nothing is written to
@@ -509,6 +532,8 @@ public static void restoreSystemProperties(
509
532
* @return text that is written to {@code System.err} by the statement.
510
533
* @throws Exception any exception thrown by the statement.
511
534
* @see #tapSystemOut(Statement)
535
+ * @see #tapSystemErrAndOut(Statement)
536
+ * @see #tapSystemErrAndOutNormalized(Statement)
512
537
* @since 1.0.0
513
538
*/
514
539
public static String tapSystemErr (
@@ -541,6 +566,8 @@ public static String tapSystemErr(
541
566
* @return text that is written to {@code System.err} by the statement.
542
567
* @throws Exception any exception thrown by the statement.
543
568
* @see #tapSystemOut(Statement)
569
+ * @see #tapSystemErrAndOut(Statement)
570
+ * @see #tapSystemErrAndOutNormalized(Statement)
544
571
* @since 1.0.0
545
572
*/
546
573
public static String tapSystemErrNormalized (
@@ -550,6 +577,80 @@ public static String tapSystemErrNormalized(
550
577
.replace (lineSeparator (), "\n " );
551
578
}
552
579
580
+ /**
581
+ * Executes the statement and returns the text that was written to
582
+ * {@code System.err} and {@code System.out} by the statement.
583
+ * <pre>
584
+ * @Test
585
+ * void application_writes_text_to_System_err_and_out(
586
+ * ) throws Exception {
587
+ * String text = tapSystemErrAndOut((){@literal ->} {
588
+ * System.err.print("text from err");
589
+ * System.out.print("text from out");
590
+ * });
591
+ * assertEquals("text from errtext from out", text);
592
+ * }
593
+ * </pre>
594
+ *
595
+ * @param statement an arbitrary piece of code.
596
+ * @return text that is written to {@code System.err} and {@code System.out}
597
+ * by the statement.
598
+ * @throws Exception any exception thrown by the statement.
599
+ * @see #tapSystemErrAndOutNormalized(Statement)
600
+ * @see #tapSystemErr(Statement)
601
+ * @see #tapSystemErrNormalized(Statement)
602
+ * @see #tapSystemOut(Statement)
603
+ * @see #tapSystemOutNormalized(Statement)
604
+ * @since 1.2.0
605
+ */
606
+ public static String tapSystemErrAndOut (
607
+ Statement statement
608
+ ) throws Exception {
609
+ TapStream tapStream = new TapStream ();
610
+ executeWithSystemErrReplacement (
611
+ tapStream ,
612
+ () -> executeWithSystemOutReplacement (
613
+ tapStream ,
614
+ statement
615
+ )
616
+ );
617
+ return tapStream .textThatWasWritten ();
618
+ }
619
+
620
+ /**
621
+ * Executes the statement and returns the text that was written to
622
+ * {@code System.err} and {@code System.out} by the statement. New line
623
+ * characters are replaced with a single {@code \n}.
624
+ * <pre>
625
+ * @Test
626
+ * void application_writes_mutliple_lines_to_System_err_and_out(
627
+ * ) throws Exception {
628
+ * String text = tapSystemErrAndOutNormalized((){@literal ->} {
629
+ * System.err.println("text from err");
630
+ * System.out.println("text from out");
631
+ * });
632
+ * assertEquals("text from err\ntext from out\n", text);
633
+ * }
634
+ * </pre>
635
+ *
636
+ * @param statement an arbitrary piece of code.
637
+ * @return text that is written to {@code System.err} and {@code System.out}
638
+ * by the statement.
639
+ * @throws Exception any exception thrown by the statement.
640
+ * @see #tapSystemErrAndOut(Statement)
641
+ * @see #tapSystemErr(Statement)
642
+ * @see #tapSystemErrNormalized(Statement)
643
+ * @see #tapSystemOut(Statement)
644
+ * @see #tapSystemOutNormalized(Statement)
645
+ * @since 1.2.0
646
+ */
647
+ public static String tapSystemErrAndOutNormalized (
648
+ Statement statement
649
+ ) throws Exception {
650
+ return tapSystemErrAndOut (statement )
651
+ .replace (lineSeparator (), "\n " );
652
+ }
653
+
553
654
/**
554
655
* Executes the statement and returns the text that was written to
555
656
* {@code System.out} by the statement.
@@ -568,6 +669,8 @@ public static String tapSystemErrNormalized(
568
669
* @return text that is written to {@code System.out} by the statement.
569
670
* @throws Exception any exception thrown by the statement.
570
671
* @see #tapSystemErr(Statement)
672
+ * @see #tapSystemErrAndOut(Statement)
673
+ * @see #tapSystemErrAndOutNormalized(Statement)
571
674
* @since 1.0.0
572
675
*/
573
676
public static String tapSystemOut (
@@ -600,6 +703,8 @@ public static String tapSystemOut(
600
703
* @return text that is written to {@code System.out} by the statement.
601
704
* @throws Exception any exception thrown by the statement.
602
705
* @see #tapSystemErr(Statement)
706
+ * @see #tapSystemErrAndOut(Statement)
707
+ * @see #tapSystemErrAndOutNormalized(Statement)
603
708
* @since 1.0.0
604
709
*/
605
710
public static String tapSystemOutNormalized (
0 commit comments