Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Mockito Essentials
Mockito Essentials

Mockito Essentials: A practical guide to get you up and running with unit testing using Mockito

Arrow left icon
Profile Icon Acharya
Arrow right icon
€17.98 €19.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (4 Ratings)
eBook Oct 2014 214 pages 1st Edition
eBook
€17.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Arrow left icon
Profile Icon Acharya
Arrow right icon
€17.98 €19.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (4 Ratings)
eBook Oct 2014 214 pages 1st Edition
eBook
€17.98 €19.99
Paperback
€24.99
Subscription
Free Trial
eBook
€17.98 €19.99
Paperback
€24.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Mockito Essentials

Chapter 2. Socializing with Mockito

"The significant problems that exist in the world today cannot be solved by the level of thinking that created them."

– Albert Einstein

This chapter distills the Mockito framework to its main core and provides technical examples. No previous knowledge of Mocking is necessary.

The following topics are covered in this chapter:

  • Exploring Mockito
  • Working with Mockito
  • Understanding the Mockito architecture

The Exploring Mockito section covers the unit test qualities and significance of Mockito in unit testing.

The Working with Mockito section explicates the Mockito framework and covers the following topics:

  • Stubbing method calls
  • Verifying method invocation
  • Matching arguments
  • Answering method calls

The Understanding the Mockito architecture section explains the internal architecture of Mockito.

Exploring Mockito

Mockito is an open source mocking framework for Java. Mockito comes under the MIT license. The MIT license says that anybody can use the software free of charge and can use, copy, modify, merge, publish, distribute, and sell the software.

In Chapter 1, Exploring Test Doubles, we read about test doubles, spies, stubs, and mock objects. Test doubles replicate the external dependencies so that the code under test can interact with its external dependencies and allow you to isolate code from its dependencies to test them on a standalone basis. Mockito streamlines the creation and management of external dependencies and allows mock object creation, verification, stubbing, and spying on real objects. To learn more about Mockito, visit the following links:

Exploring unit test qualities

Writing clean, readable, and maintainable unit test cases (JUnit, TestNG) is an art; just like writing clean code. A well...

Working with Mockito

This section provides an overview of Mockito. Here is the official Mockito logo:

Working with Mockito

The following topics are covered in this section:

  • Configuring Mockito
  • Stubbing method calls
  • Throwing exceptions
  • Matching method arguments
  • Verifying method calls

Download the latest Mockito binary ZIP folder from the following link and add it to the project dependency. The recommended channel for getting Mockito is Maven (or Gradle), or download it directly from the central Maven repository if you need to get the JAR files manually from either http://central.maven.org/maven2/org/mockito/mockito-all/ or http://central.maven.org/maven2/org/mockito/mockito-core/.

As of April 2014, the latest Mockito version is 1.9.5.

Adding a Mockito dependency

The following steps describe how Mockito JAR files can be added as project dependency:

  1. Extract the JAR files into a folder.
  2. Launch Eclipse.
  3. Create an Eclipse project named 3605OS_Socializing_with_Mockito.
  4. Go to the Libraries tab in the project's build path.
  5. Click...

Understanding the Mockito architecture

Mockito applies the proxy design pattern to create mock objects. For concrete classes, Mockito internally uses CGLib to create proxy stubs. CGLib is used to generate dynamic proxy objects and intercept field access. The following URL talks about CGLib:

https://github.com/cglib/cglib

The following sequence diagram depicts the call sequence. The ClassImposterizer class is a singleton class. This class has a createProxyClass method for generating a source using CGLib. Finally, it uses reflection to create an instance of the proxy class. Method calls are stubbed using the callback API of MethodInterceptor.

Understanding the Mockito architecture

The MethodInterceptor class acts as a Java reflection class, java.lang.reflect.InvocationHandler. Any method call on a mock object (proxy) is handled by a MethodInterceptor instance.

We'll create a custom mocking framework to handle external dependencies. We'll use the Java reflection framework's dynamic proxy object-creation API. The java...

Summary

This chapter covered the Mockito overview, unit test qualities, and the significance of Mockito in unit testing. It explained and provided examples of stubbing, answering, throwing exceptions, argument matcher, and method call verification. It also covered the Mockito architecture.

By now, you should be able to verify method calls, stub methods, and throw exceptions using the Mockito framework.

The next chapter, Accelerating Mockito, focuses on advanced Mockito topics.

Left arrow icon Right arrow icon

Description

This book is ideal for developers who have some experience in Java application development as well as some basic knowledge of test doubles and JUnit testing. This book also introduces you to the fundamentals of JUnit testing, test doubles, refactoring legacy code, and writing JUnit tests for GWT and web services.

What you will learn

  • Explore test doubles and work with dummy, spy, fake, stub, and mock objects
  • Uncover the Mockito architecture and build a custom mocking framework
  • Mock, stub, and spy external code dependencies using Mockito
  • Practice Behaviordriven Development (BDD) with Mockito
  • Make legacy code testable by mocking and spying dependencies
  • Mock GWT and web service dependencies using Mockito
  • Discover argument captors, inline stubbing, and resetting mock objects

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 24, 2014
Length: 214 pages
Edition : 1st
Language : English
ISBN-13 : 9781783983612
Category :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 24, 2014
Length: 214 pages
Edition : 1st
Language : English
ISBN-13 : 9781783983612
Category :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Can$ 96.97
MOCKITO COOKBOOK
€35.99
Mockito Essentials
€24.99
MASTERING UNIT TESTING USING MOCKITO AND JUNIT
€35.99
Total Can$ 96.97 Stars icon

Table of Contents

8 Chapters
1. Exploring Test Doubles Chevron down icon Chevron up icon
2. Socializing with Mockito Chevron down icon Chevron up icon
3. Accelerating Mockito Chevron down icon Chevron up icon
4. Behavior-driven Development with Mockito Chevron down icon Chevron up icon
5. Unit Testing the Legacy Code with Mockito Chevron down icon Chevron up icon
6. Developing SOA with Mockito Chevron down icon Chevron up icon
7. Unit Testing GWT Code with Mockito Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(4 Ratings)
5 star 0%
4 star 100%
3 star 0%
2 star 0%
1 star 0%
Timothy Bish Mar 11, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book offers a nice quick dive into the Mockito framework for testing your code. It serves as a great introduction for the Mockito novice, covering not only the basic workings of the framework but also many concepts surrounding when and why to use a mocking framework.There are a couple of chapters that spend a bit to much time being tutorials for other topics but I can understand why that might be needed and those can be skimmed a bit if things like GWT development doesn't really interest you. The chapters that do cover core Mockito material are thorough and provide a number of useful tips and tricks. For the Mockito novice this book is a definite must read, but could use more chapters covering more advanced topics surrounding the framework if targeted at seasonal Mockito users.The samples in the book cover the topic well and work which is a pleasant surprise these days.
Amazon Verified review Amazon
Alexandros Koufoudakis Jan 25, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Mockito Essentials is a good introductory book for those who start using Mockito and for those, who start their journey with unit testing in general.The book is mainly focused on Mockito framework. It starts with quite a detailed introduction in what unit testing and test doubles are. Then, it gradually introduces features of the Mockito framework, starting with simple cases and finishing with special techniques to unit test SOAs and applications based on GWT. I guess those technologies were chosen to demonstrate how you can use the framework in the real life. Somewhere in between you will find the tips and tricks, along with explanations, on how Mockito can improve your unit testing techniques. You will also find two very useful chapters. One of them is focused on behavior driven development and how you can apply Mockito to carry out BDD. Another describes how one can use Mockito to unit test their legacy code. Both chapters are very helpful and a must read for this book, along with the chapter one. Of course, to understand how Mockito works, you have to read chapters two and three.The book also includes working code examples, although I don’t agree with the approach of some of those examples.I would recommend Mockito Essentials not only, because it is a good read, but also, because it one of the best books I’ve read about Mockito framework.
Amazon Verified review Amazon
Dustin Marx Jan 06, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
"Mockito Essentials" consists of seven chapters and approximately 190 substantive pages. Chapter 1 is not specific to Mockito, but provides an overview of test doubles and explains why they are important in effective unit tests. Chapters 2 and 3 cover "core" Mockito concepts and syntax. Chapter 5 covers using Mockito with legacy applications, using PowerMock, and refactoring legacy code so that it can be mocked with Mockito. Chapter 4 covers use of Mockito with Behavior-Driven Development (BDD). Chapter 6 discusses generation of REST-based web services with JAX-RS and SOAP-based web services with JAX-WS and how to test them with Mockito. Chapter 7 demonstrates building of an application with Google Web Toolkit and Spring MVC and testing it with Mockito and PowerMockito.The chapters on web services and GWT spend a lot of time covering how to develop the sample applications before showing how to test them and this may or may not be desirable to readers. Chapters 1, 2, 3, and 5 could be considered "core" Mockito chapters that one might expect from a book on "Mockito Essentials." Chapter 5 is of most interest for those practicing or interested in practicing BDD. Chapter 6 is of most interest to those developing web services and Chapter 7 is of most interest to those developing Google Web Toolkit applications.The PDF version of "Mockito Essentials" has code listings with black text on white background with no color syntax or line numbers. Some screen snapshots and graphics are in color and some are grayscale. I liked that there were numerous links to additional online resources provided.Overall, I liked the PDF edition of Mockito Essentials that I reviewed. Although most of the text is clear, there were enough typos and awkward sentences to lead me to believe that another round of edits could have been beneficial. The Mockito project provides good, clear documentation (I especially like its API documentation), but "Mockito Essentials" goes past that and combines other related and peripheral technologies in the discussion.
Amazon Verified review Amazon
A. Iqbal Dec 27, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I really have enjoyed this book.The writing is clear most of the time, and easy to follow. The chapters that i was really looking forward to reading were, BDD with Mockito and testing legacy code with Mockito. I think the chapters provide a good insight and good foundation on using Mockito for the purpose in hand, however I would have liked more depth and more chapters.But for what i got from the book, i would definitly reccommed if your starting out with Mockito.Overall a good easy read.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.