1private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
2private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
3private final PrintStream originalOut = System.out;
4private final PrintStream originalErr = System.err;
5
6@Before
7public void setUpStreams() {
8 System.setOut(new PrintStream(outContent));
9 System.setErr(new PrintStream(errContent));
10}
11
12@After
13public void restoreStreams() {
14 System.setOut(originalOut);
15 System.setErr(originalErr);
16}
17
18sample test cases:
19
20@Test
21public void out() {
22 System.out.print("hello");
23 assertEquals("hello", outContent.toString());
24}
25
26@Test
27public void err() {
28 System.err.print("hello again");
29 assertEquals("hello again", errContent.toString());
30}