Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading System.out Java Test

Tags:

java

I have a task to test a console application. I can access one method, but would also like to check that the string formatting is working correctly.

As this method does not return a string, but rather prints to the console, is there a way I can intercept the list printed line?

Does it even make sense to test this sort of thing?

like image 664
james_dean Avatar asked Dec 01 '22 16:12

james_dean


1 Answers

Here is a sample how to catch output to the System.out:

java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();    
System.setOut(new java.io.PrintStream(out));    

System.out.println("Test output");    
System.err.println("Out was: " + out.toString());
like image 65
AnatolyG Avatar answered Dec 20 '22 13:12

AnatolyG