The Disco Blog

Can you dig it?

Book Review: Instant Mockito

Recently, the good folks over at Packt Publishing gave me a copy of their newly published Instant Mockito, by Marcin Grzejszczak. Packt’s Instant series are really enjoyable. The premise of these books is that they’re short and sweet. They’re slightly more than a tutorial; they get you up and running quickly while throwing in a few more facets that go beyond the typical tutorial.

Instant Mockito is a quick read at 92 pages; what’s more, Marcin does a great job of keeping a consistent example domain alive throughout the entire book. The big picture is how to use Mockito with JUnit, testing a fictitious restaurant ordering system.

The format of Packt’s Instant books are all similar — the first part is a quick “up and running” that essentially encapsulates the basic tutorial you can find on a project’s home page. The meat of an Instant book comes next, where the author dives into a number of features they feel are important to grasp a particular subject.

In the case of Marcin’s book, he elaborates on 8 key Mockito subjects:

  • Performing argument matching
  • Stubbing multiple calls
  • Working with void methods and thrown exceptions
  • Stubbing with a custom answer
  • Verifying behavior (including argument capturing, verifying call order, and working with asynchronous code)
  • Doing partial mocking (spying)
  • Reducing boilerplate code with annotations
  • Taking advantage of advanced mocks configuration

Again, each subject expands upon a waiter-taking-an-order domain. As this is a book focusing on Mockito, the mocking aspects are straightforward — this isn’t a book on how to test poorly written code, for example, so the code under test in this case uses dependency injection. This makes it easy to grasp Mockito subjects without having to delve into the intricacies of terrible code (ironically, I often times find myself employing Mockito when dealing with poorly written code).

Speaking of poorly written code: the only thing I did not particularly enjoy about the book was the formatting of the code. Code formatting has always been problematic in books, however, many publishers have figured out how to properly convey code (in particular, O’Reilly books do a good job).

In the case of Instant Mockito, the code examples (at least reading the epub version on my iPad) have such narrow margins that each logical line of code is often broken into 3 or more lines (and dashes are used to signify a line break). While it certainly doesn’t render the code unreadable, it makes instant code comprehension challenging. I found myself having to read through each code example multiple times just to get the basic idea.

Code formatting aside, Marcin’s book was an easy read and the 8 features he feels every Mockito user should know are well explained. As a user of Mockito, I couldn’t agree more with the relevance of these features. If you enjoy quick reads, want to learn the basics of mocking in JUnit with Mockito, and you can put up with inadequate code formatting, then go ahead and give Instant Mockito a read!