Unit testing is tedious, but it has saved my bacon so many times already that even though it eats my time I still religiously create unit test cases for every important business logic use cases. I’ve been using Spring’s integration with JUnit and its very cool transaction support for test cases. Unfortunately with this approach, I have to craft test data to test every single code path for complex business logics. Although the setup, tearDown and rollback upon completion of the test case is handy, I still have to carefully craft the data.
Enter EasyMock, I know i’m late in the game with mock objects, but in the quest to meet deadlines and still have close to 100% test coverage (well I try to anyway), I haven’t had the time to actually check out what’s new in the TDD world.
At its most basic level, it allows you to record and replay the expected method calls and return values for methods that are being used by the object/method that you are currently testing. Aside from mocking interface classes you can also partially mock concrete classes (meaning mock only selected methods) using EasyMock’s Class Extension. With this, you have fine grain control on how each and every test case will behave.
Check it out at http://www.easymock.org
I’ll post a tutorial in my next blog post. There’s so much stuff in there that I haven’t used that I’ll have to start a new tutorial series just to cover this very cool mock framework.