Book review: Sujoy Acharya – Mockito essentials

A few weeks back I was contacted by Packt Publishing to review a relatively newly published book: Mockito essentials. Without any further talk, let’s see:

I tried to read the book like I had never seen Mockito, or encountered any of the principles of mocking before. Under these conditions some of the chapters were very useful, others less. At many points I had the impression that examples int this book are (maybe) a bit too advanced for beginners, but not so useful for pros. This is mainly because people completely new to mocking/stubbing/faking may get lost in some rather complex pieces of code, while seasoned mockers are reading more advanced books.

But let’s take it chapter-wise.

Right in the first chapter, these ambivalent feelings strike. I liked it very much how the author went through all the different types of test doubles. These concepts, I think, are absolutely mandatory for someone to catch up with interactions based testing. Also, there is an excellent description about the concept of unit tests – what is a (good) unit test and what is not – many people get it wrong. However, in some cases I felt the example codes were too complex; a lot of collaborating classes with hard to follow logic (Mark object having a Student object, all these aggregated by a Teacher class). I appreciate these are not real world solutions, I only think somewhat simpler examples could have done better.

This is also true for the next chapter, when Mockito basics are introduced. The very first example is about a web application (Web GUI) with many annotations and different techniques, like request-response building, DAO pattern and JSON. I think as a very first example something technology-independent and more accessible could have been a better choice. When it comes to the description of different topics, like matchers, Mockito limitations, verifications, all of a sudden it is very good and easy to catch once again. (Not like the last part of the chapter – with an own mock framework implementation – which has nothing to do with Mockito).

The best part of the book, in my honest opinion, is chapter three – on accelerated mocking. It has a very very good example to walk you through different possibilities of Mockito – a front controller servlet. I think this example is much better than the others: dependencies are more natural, the code is not too complex – still something complex enough to encounter at daily work. Exception handling, stubbing void methods, annotations – all described in an easy-to-follow manner. This chapter even discusses some features of Mockito that are not very commonly used – so I think this is a good reference guide.

The book has a chapter on BDD with Mockito. I am not a big fan of doing this – I rather think, one should grab JBehave or Cucumber for true BDD – but it is worth taking a look at it, to get a taste/compare with true BDD frameworks.

The chapter on legacy code is again, very good. Useful tips on how to deal with spaghetti code, how to make things testable. It’s not only about Mockito – some examples don’t even use Mockito – so this is a complete arsenal to fight legacy code. It also introduces PowerMock with small, comprehensible, to the point examples. Very nice chapter.

The remaining two chapters deal with “mocking web services” and “mocking GWT”. I don’t think these add too much value to the book, as they rather focus on the technology (SOAP, REST, GWT) than mocking. If we take the characteristic classes, annotations and configuration out of the context, and apply some good OO principles (decoupling, separation of concerns) we get down to the very same techniques presented in the previous chapters. It felt a bit strange that creating the example application takes three times more pages than the solutions presented.

Taking all these things into account, I think it can be a useful book (mainly due to those the two chapters I’ve emphasized); I am giving this book three stars out of five.

Author: tamasgyorfi

Senior software engineer, certified enterprise architect and certified Scrum master. Feel free to connect on Twitter: @tamasgyorfi

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s