« HelloWorld v 2.0

November 8, 2008 • ☕️ 1 min read

Once upon a time we used to write:

public static void main(String[] args) {
System.out.println("Hello World");
}

Now I prefer:

@Test
public void shouldSayHelloToTheGivenName() throws Exception {
// given
PrintStream mockStream = mock(PrintStream.class);
Greeter greeter = new Greeter(mockStream);
// when
greeter.helloworld("toni");
// then
verify(mockStream).println("Hello world, toni");
}

The test is of course using the beautiful test double framework Mockito