Say I have some method or operation ReturnType method(ArgType argument)
in Java. I want to make it so that invoking method
will not cause some message to be written to System.out, and afterwards the behaviour should be as before. My naive idea for something doing this would be to write the following code:
PrintStream systemout = System.out
System.setOut(new PrintStream(new ByteArrayOutputStream()))
method(arg)
System.setOut(systemout)
But it does not seem to work. Is there some straightforward way of accomplishing this?