title: Test console output using JUnit
body: <p>I'm trying to test console output of another program using JUnit. I followed the answers given in <a href="https://stackoverflow.com/questions/1119385/junit-test-for-system-out-println">here</a></p>
  
  <p>Here is my JUnit class</p>
  
  <pre><code>import static org.junit.Assert.*;
  import java.io.ByteArrayOutputStream;
  import java.io.PrintStream;
  import org.junit.*;
  
  public class MainTest {
          private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
          private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
  
          @Before
          public void setup() {
                  System.setOut(new PrintStream(outContent));
                  System.setErr(new PrintStream(errContent));
          }
  
          @Test
          public void test01_initTest() {
                  String[] arguments = {"a", "b"};
                  Main.main(arguments);
                  String expected = "hello";
                  assertTrue(expected == outContent.toString());
          }
  
          @After
          public void cleanUpStreams() {
              System.setOut(null);
              System.setErr(null);
          }
  }
  </code></pre>
  
  <p>When I run this program in eclipse, neither do I see the console output and the test run doesn't end. However if I printout on console directly from test01_initTest(), the test passes. To check if Main.main() is printing out I also tried the below code and I do see the console output in this case</p>
  
  <pre><code>import static org.junit.Assert.*;
  import org.junit.*;
  
  public class MainTest {
          @Test
          public void test01_initTest() {
                  String[] arguments = {"a", "b"};
                  Main.main(arguments);
          }
  }
  </code></pre>
  
  <p>I tired a lot of things but I'm unable to figure out what I'm doing wrong here. Can someone please help me out.</p>