Sunday 14 January 2018

java - In JUnit testing is it possible to check the method System.out.println()













Is it
possible to check, through JUnit testing, if the method System.out.println("One, Two"),
actually prints One, Two?


itemprop="text">
class="normal">Answer



A better
idea would be to change your application to be more testable by not using System.out
directly.





  • If
    the method you are trying to test is designed to write output for end users, modify it
    or the enclosing class so that the destination OutputStream or PrintWriter or whatever
    is a parameter.


  • On the other hand, if
    the method is producing log output, use a proper logging framework ... and consider
    whether it is worthwhile to unit test the log output at
    all.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...