The Source for Java Technology Collaboration
User: Password:



Tom White

Tom White's Blog

jMock 2 and my Java Unit Testing Toolkit

Posted by tomwhite on April 11, 2007 at 06:16 AM | Comments (2)

The long-awaited final version of jMock 2 was released today. There are some big changes since version one. For example, you can now write

Cat cat = mock(Cat.class);

and then set expectations on the returned cat object itself:

checking(new Expectations() {{
    one(cat).miaow();
}});

(This means that the miaow method is expected to be called exactly once on the cat object.)

This change means that you can refactor your method names without breaking your tests, unlike jMock 1 where the method names were strings:

Mock mockCat = mock(Cat.class);
mockCat.expects(once()).method("miaow")

You may have noticed that the new syntax is a little weird (check out the double braces, and how e.g. will modifiers need new statements), and it may take a little getting used to, but I think it's worth it for the IDE completion and refactoring support. For example, in test driven mode I can write the name of the method that doesn't yet exist then get my IDE to create the method for me.

The other nice thing is the integration with Hamcrest (a library of matchers that I've written about before). My toolkit for writing new unit tests now includes:

  1. Hamcrest for making flexible assertions.
  2. jMock 2 for defining expected behaviour.
  3. Responsible Naming for understanding what the test does.

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • EasyMock's been able to do this for years now.

    http://www.easymock.org

    It's the reason I've constantly chosen EasyMock over jMock...the refactoring support (can refactor w/o breaking mocks). And it's in a much cleaner syntax than what you've shown for jMock.

    Posted by: aochsner on April 11, 2007 at 09:17 PM

  • Some more on the topic:

    http://www.coderslog.com/Unit_Testing_101

    Thank you for the time

    Posted by: screenedtwenty on June 04, 2007 at 05:51 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds