8 Nov 2008, 7:01pm
work:
by toni

3 comments

HelloWorld v 2.0

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

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!

 
*name

*e-mail

web site

leave a comment