Book Image

Mockito Cookbook

By : Marcin Grzejszczak
Book Image

Mockito Cookbook

By: Marcin Grzejszczak

Overview of this book

Table of Contents (17 chapters)
Mockito Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

According to Google Trends, Mockito, compared to its main Java mocking framework competitors, EasyMock and jMock, has been the most widely used since 2011 and this trend has been upward ever since. Given its extremely simple and elegant API, Mockito gives you the possibility to test your application in a readable manner. Furthermore, it's syntax is so intuitive that you'll learn it in no time at all.

The very concept behind this book is to give the reader the possibility to use Mockito in order to write beautiful and comprehensive tests. The Mockito documentation as such is of very high quality, so you should always, regardless of the tool you are using, refer to it when in doubt. This book is an extension to this documentation since it covers its content but puts it in a real-life example. Where the Mockito documentation proves that the library, as such, is doing what it is supposed to do, you can come to a point where you don't actually know how to use it versus your production code. Worry not! Mockito Cookbook comes to the rescue. This book contains solutions to more than 60 problems that you may encounter throughout your Mockito testing endeavor. You will learn how to write tests that become the living documentation of your code. You will become A Mockito expert. (Since the book also explains some Mockito internals you might even be tempted to become its contributor!) And hopefully, your tests will become an example to be followed by your colleagues.

What this book covers

Chapter 1, Getting Started with Mockito, covers the Mockito configuration for JUnit and TestNG and some of its experimental features.

Chapter 2, Creating Mocks, presents numerous ways to create mocks.

Chapter 3, Creating Spies and Partial Mocks, covers the process of instantiating spy objects and partial mocks.

Chapter 4, Stubbing Behavior of Mocks, shows how to stub the method executions of mock objects.

Chapter 5, Stubbing Behavior of Spies, presents ways to stub the method executions of spies.

Chapter 6, Verifying Test Doubles, covers the process of behavior verification of test doubles.

Chapter 7, Verifying Behavior with Object Matchers, shows how to confirm that your application works as it should using Hamcrest or AssertJ.

Chapter 8, Refactoring with Mockito, covers the process of easily refactoring your production and test code, thanks to Mockito.

Chapter 9, Integration Testing with Mockito and DI Frameworks, presents ways to inject mocks into your Spring– or Guice–based applications.

Chapter 10, Mocking Libraries Comparison, shows the differences and similarities between several mocking libraries and Mockito.

What you need for this book

In order to run the code presented in this book, you will need Java Development Kit 1.6 or newer, Mockito Version 1.9.5 appended to your classpath, and in the majority of the presented tests, AssertJ Version 1.6.0. The GitHub repository that contains the code has a configuration ready for use with Gradle and Maven, so you need either of these installed on your machine to run the tests.

Who this book is for

If you are a developer who either has never used Mockito or want to extend your knowledge about this framework, this the book for you. This book not only shows you how to solve issues with Mockito, but also dives into the internals of Mockito in order to help you understand the tool better. The book can also be addressed by test enthusiasts who want to see another approach to the tests that are behavior-driven.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Where NewIdentityCreator contains the logic for generating new identity."

A block of code is set as follows:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. The project setup for usage with Maven or Gradle with all of the code from the book and some additional tests and use cases is also present on GitHub at https://github.com/marcingrzejszczak/mockito-cookbook.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.