I'm trying to test console output of another program using JUnit. I followed the answers given in here
Here is my JUnit class
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);
}
}
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
import static org.junit.Assert.*;
import org.junit.*;
public class MainTest {
@Test
public void test01_initTest() {
String[] arguments = {"a", "b"};
Main.main(arguments);
}
}
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.