I assume your function prints its output via System.out.println
.
System.out.println
means "call the println()
method on the static field out
of the class System
.
This out
field is of type PrintStream
, and can be reassigned by calling System.setOut()
and passing it a new PrintStream
of your choice, which you can then use to read the output generated by your function (you may want to take a look at the PipedOutputStream
/PipedInputStream
classes to achieve that).
Be aware though, that you will also trap the output of every other method that uses System.out.println
(including any library you may also use in your application) !