December 14, 2019
Hot Topics:

Squeezing More Guice from Your Tests with EasyMock

  • July 12, 2007
  • By Dick Wall
  • Send Email »
  • More Articles »

Wrapping Up

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.

Is Guice then a one trick pony? Clearly it is great for testing, but is that all it is useful for? Emphatically no! Guice was actually created with quite a different use in mind, that of breaking up dependencies in large systems, and it is that I shall talk about in the next article. From my point of view, this is a far more interesting and important use of Guice. Tests are a great way of grasping what Guice can do, but once you understand the techniques of dependency injection, you can move on to more advanced situations where it can be of use.

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

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date