JUnit test for System.out.println()

using ByteArrayOutputStream and System.setXXX is simple: private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; private final PrintStream originalErr = System.err; @Before public void setUpStreams() { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); } @After public void restoreStreams() { System.setOut(originalOut); System.setErr(originalErr); } sample test cases: @Test public … Read more

How to run JUnit test cases from the command line

For JUnit 5.x it’s: java -jar junit-platform-console-standalone-<version>.jar <Options> Find a brief summary at https://stackoverflow.com/a/52373592/1431016 and full details at https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher For JUnit 4.X it’s really: java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] But if you are using JUnit 3.X note the class name is different: java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name] You might need to … Read more