import junit.framework.TestCase; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class HelloWorldTest extends TestCase { public void testHelloWorld() { final var oldSystemOut = System.out; final var outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); try { HelloWorld.main(null); } finally { System.setOut(oldSystemOut); } assertEquals("Hello world!" + System.lineSeparator(), outputStream.toString()); } }