File tree Expand file tree Collapse file tree 3 files changed +89
-1
lines changed
approvaltests-tests/src/test/java/org/approvaltests/utils
approvaltests/src/main/java/org/approvaltests/utils
internal_documentation/micro_features Expand file tree Collapse file tree 3 files changed +89
-1
lines changed Original file line number Diff line number Diff line change 1+ package org .approvaltests .utils ;
2+
3+ import org .approvaltests .core .Options ;
4+ import org .approvaltests .reporters .AutoApproveReporter ;
5+ import org .approvaltests .reporters .UseReporter ;
6+ import org .junit .jupiter .api .Test ;
7+
8+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9+
10+ public class ConsoleOutputTests
11+ {
12+ @ Test
13+ @ UseReporter (AutoApproveReporter .class )
14+ void testVerifyOutput ()
15+ {
16+ var expected = """
17+ Hello, World!
18+ No newline
19+ """ ;
20+ try (ConsoleOutput console = new ConsoleOutput ())
21+ {
22+ System .out .println ("Hello, World!" );
23+ System .out .print ("No " );
24+ System .out .print ("newline" );
25+ console .verifyOutput (new Options ().inline (expected ));
26+ }
27+ }
28+ @ Test
29+ @ UseReporter (AutoApproveReporter .class )
30+ void testVerifyError ()
31+ {
32+ var expected = """
33+ Error message
34+ No newline
35+ """ ;
36+ try (ConsoleOutput console = new ConsoleOutput ())
37+ {
38+ System .err .println ("Error message" );
39+ System .err .print ("No " );
40+ System .err .print ("newline" );
41+ console .verifyError (new Options ().inline (expected ));
42+ }
43+ }
44+ @ Test
45+ @ UseReporter (AutoApproveReporter .class )
46+ void testVerifyAll ()
47+ {
48+ var expected = """
49+ Output:
50+ Standard output
51+
52+ Error:
53+ Error output
54+ """ ;
55+ try (ConsoleOutput console = new ConsoleOutput ())
56+ {
57+ System .out .println ("Standard output" );
58+ System .err .println ("Error output" );
59+ console .verifyAll (new Options ().inline (expected ));
60+ }
61+ }
62+ }
Original file line number Diff line number Diff line change @@ -83,6 +83,24 @@ public void verifyError(Options options)
8383 {
8484 Approvals .verify (getError (), options );
8585 }
86+ /**
87+ * Verifies both captured standard output and error using ApprovalTests.
88+ * This is a convenience method that calls Approvals.verify() on both the output and error combined.
89+ */
90+ public void verifyAll ()
91+ {
92+ verifyAll (new Options ());
93+ }
94+ /**
95+ * Verifies both captured standard output and error using ApprovalTests with options.
96+ * This is a convenience method that calls Approvals.verify() on both the output and error combined.
97+ * @param options The options to use for verification
98+ */
99+ public void verifyAll (Options options )
100+ {
101+ String combined = "Output:\n " + getOutput () + "\n Error:\n " + getError ();
102+ Approvals .verify (combined , options );
103+ }
86104 /**
87105 * Restores the original System.out and System.err streams.
88106 * This method is automatically called when used in a try-with-resources block.
Original file line number Diff line number Diff line change @@ -18,6 +18,9 @@ Allows users to capture and verify console output (both System.out and System.er
18183 . ** getError()** : Returns captured standard error as a string
19194 . ** verifyOutput()** : Convenience method that calls Approvals.verify() on captured output
20205 . ** verifyError()** : Convenience method that calls Approvals.verify() on captured error
21+ 5 . ** verifyAll()** : Convenience method that calls Approvals.verify() on both the output and error
22+
23+
21246 . ** close()** : Restores original System.out and System.err streams
2225
2326## Usage Example
@@ -30,4 +33,9 @@ try (ConsoleOutput console = new ConsoleOutput()) {
3033
3134## Integration
3235- Works seamlessly with ApprovalTests
33- - Automatically handles stream restoration even if exceptions occur
36+ - Automatically handles stream restoration even if exceptions occur
37+
38+ ## Tests
39+ 1 . Test verifyOutput
40+ 2 . Test verifyError
41+ 3 . Test verifyAll
You can’t perform that action at this time.
0 commit comments