66
77import java .io .ByteArrayOutputStream ;
88import java .io .PrintStream ;
9+ import java .io .UnsupportedEncodingException ;
910import org .junit .jupiter .api .AfterEach ;
1011import org .junit .jupiter .api .BeforeEach ;
1112import org .junit .jupiter .api .Test ;
@@ -16,8 +17,8 @@ class ConsoleExportProgressReporterTest {
1617 private final PrintStream originalOut = System .out ;
1718
1819 @ BeforeEach
19- void setUpStreams () {
20- System .setOut (new PrintStream (outContent ));
20+ void setUpStreams () throws UnsupportedEncodingException {
21+ System .setOut (new PrintStream (outContent , true , "UTF-8" ));
2122 }
2223
2324 @ AfterEach
@@ -26,22 +27,23 @@ void restoreStreams() {
2627 }
2728
2829 @ Test
29- void testStartMessageIncludesExportFilePath () {
30+ void testStartMessageIncludesExportFilePath () throws UnsupportedEncodingException {
3031 new ConsoleExportProgressReporter ("output/test.csv" );
31- String output = outContent .toString ();
32+ String output = outContent .toString ("UTF-8" );
3233 assertTrue (output .contains ("📤 Starting export" ), "Expected start message" );
3334 assertTrue (
3435 output .contains ("📁 Exporting data to file: output/test.csv" ), "Expected file path info" );
3536 }
3637
3738 @ Test
38- void testCompletionMessageIncludesFilePathAndDuration () throws InterruptedException {
39+ void testCompletionMessageIncludesFilePathAndDuration ()
40+ throws InterruptedException , UnsupportedEncodingException {
3941 ConsoleExportProgressReporter reporter = new ConsoleExportProgressReporter ("target/output.csv" );
4042
4143 Thread .sleep (100 ); // Simulate work
4244
4345 reporter .reportCompletion (12345 );
44- String output = outContent .toString ();
46+ String output = outContent .toString ("UTF-8" );
4547
4648 assertTrue (
4749 output .contains ("✅ Export completed: 12,345 records exported to target/output.csv" ),
@@ -50,30 +52,31 @@ void testCompletionMessageIncludesFilePathAndDuration() throws InterruptedExcept
5052 }
5153
5254 @ Test
53- void testCompletionOnlyPrintedOnce () {
55+ void testCompletionOnlyPrintedOnce () throws UnsupportedEncodingException {
5456 ConsoleExportProgressReporter reporter = new ConsoleExportProgressReporter ("target/output.csv" );
5557
5658 reporter .reportCompletion (100 );
5759 reporter .reportCompletion (999999 ); // Should be ignored
5860
59- String output = outContent .toString ();
61+ String output = outContent .toString ("UTF-8" );
6062 int count = output .split ("Export completed" ).length - 1 ;
6163 assertEquals (1 , count , "Expected completion to be printed only once" );
6264 }
6365
6466 @ Test
65- void testReportError_shouldPrintErrorMessageWithExceptionMessage () {
67+ void testReportError_shouldPrintErrorMessageWithExceptionMessage ()
68+ throws UnsupportedEncodingException {
6669 ByteArrayOutputStream errContent = new ByteArrayOutputStream ();
6770 PrintStream originalErr = System .err ;
68- System .setErr (new PrintStream (errContent ));
71+ System .setErr (new PrintStream (errContent , true , "UTF-8" ));
6972
7073 try {
7174 String errorMessage = "Something went wrong" ;
7275 Throwable cause = new RuntimeException ("Test exception" );
7376
7477 ConsoleExportProgressReporter .reportError (errorMessage , cause );
7578
76- String output = errContent .toString ();
79+ String output = errContent .toString ("UTF-8" );
7780 assertTrue (
7881 output .contains ("❌ Export failed: " + errorMessage ), "Expected main error message" );
7982 assertTrue (output .contains ("Cause: " + cause .getMessage ()), "Expected exception message" );
@@ -83,17 +86,18 @@ void testReportError_shouldPrintErrorMessageWithExceptionMessage() {
8386 }
8487
8588 @ Test
86- void testReportError_shouldPrintMessageWithoutExceptionWhenNull () {
89+ void testReportError_shouldPrintMessageWithoutExceptionWhenNull ()
90+ throws UnsupportedEncodingException {
8791 ByteArrayOutputStream errContent = new ByteArrayOutputStream ();
8892 PrintStream originalErr = System .err ;
89- System .setErr (new PrintStream (errContent ));
93+ System .setErr (new PrintStream (errContent , true , "UTF-8" ));
9094
9195 try {
9296 String errorMessage = "Directory not found" ;
9397
9498 ConsoleExportProgressReporter .reportError (errorMessage , null );
9599
96- String output = errContent .toString ();
100+ String output = errContent .toString ("UTF-8" );
97101 assertTrue (output .contains ("❌ Export failed: " + errorMessage ), "Expected error message" );
98102 assertFalse (
99103 output .contains ("Cause:" ), "Should not print exception cause when throwable is null" );
0 commit comments