“Hello world!” in Java. Agile version.

Finally, a correct version of classic “Hello world!” program.

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.Test;

public class HelloWorld {

  @Test
  void testMainPrintsHelloWorld() {
    // given
    final var out = new ByteArrayOutputStream();
    System.setOut(new PrintStream(out));

    // when
    HelloWorld.main();

    // then
    assertThat(out.toString()).isEqualTo("Hello world!");
  }

  public static void main(final String... args) {
    System.out.print("Hello world!");
  }
}

See also https://agilemanifesto.org/