Catches STDOUT and writes it into a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String catchStdout(Runnable runnable) { | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
System.setOut(new PrintStream(outputStream)); | |
runnable.run(); | |
return outputStream.toString(); | |
} | |
@Test | |
void testCatchStdout() { | |
String s = "Hello, World"; | |
assertEquals(s + "\n", catchStdout(() -> System.out.println(s))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment