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


















3 Comments
new Toni().println(“you screwed”);
given, when, then? you screwed up toni. mocking classes you dont own? booo. i leave you alone for a few months and this is what happens.
nice idea
just setting the MockStream is missing:
..
Greeter greeter = new Greeter(mockStream);
greeting.setPrintStream(mockStream)
// when
..
Thanks,
the stream is injected through the constructor, so no need to set it!