Squeezing More Guice from Your Tests with EasyMock
If you download the accompanying source code project for this article and look at the EasyMock implementation, you will see the whole example fleshed out, and with an extra test not covered here as well; it demonstrates multiple expected calls to getTaxRateForCustomer, returning different values for different customer instances. This allows you to test that Invoice returns different totals depending on different customer instances and tax rates. You always can get pretty inventive with your testing and should do so. EasyMock makes it simple and quick to come up with all sorts of different scenarios, including test failures, exceptions, even simulating bad data conditions (what would your code do if TaxRateManager happened to return a negative number, or not a number, and so forth?).
When chatting with Bob Lee about this article, he uttered the statement "Guice and EasyMock could have been made for each other." Certainly the two in combination make for an extremely compelling solution to unit testing just about any code you can think of.
About the Author
Dick Wall is a Software Engineer at Google based in Mountain View. He also co-hosts the Java Posse podcast, a regular Java-centric news and interviews show that can be found at http://javaposse.com.
Page 5 of 5