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

This entry was posted in work and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. gaz
    Posted November 9, 2008 at 12:17 am | Permalink

    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.

  2. bosho
    Posted November 11, 2008 at 10:12 pm | Permalink

    nice idea :)

    just setting the MockStream is missing:
    ..
    Greeter greeter = new Greeter(mockStream);
    greeting.setPrintStream(mockStream)
    // when
    ..

  3. Posted November 12, 2008 at 10:35 am | Permalink

    Thanks,
    the stream is injected through the constructor, so no need to set it!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>