Mockito Cookbook


Mockito Cookbook
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
$22.95
save 15%!
Print + free eBook + free PacktLib access to the book: $70.98    Print cover: $43.99
$66.94
save 6%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Support
Sample Chapters
  • Implement best practices to perform tests with Mockito
  • Extend Mockito with other popular Java-based unit testing frameworks such as JUnit and Powermock
  • A focused guide with many recipes on testing your software using Mockito

Book Details

Language : English
Paperback : 284 pages [ 235mm x 191mm ]
Release Date : June 2014
ISBN : 1783982748
ISBN 13 : 9781783982745
Author(s) : Marcin Grzejszczak
Topics and Technologies : All Books, Application Development, Open Source


Table of Contents

Preface
Chapter 1: Getting Started with Mockito
Chapter 2: Creating Mocks
Chapter 3: Creating Spies and Partial Mocks
Chapter 4: Stubbing Behavior of Mocks
Chapter 5: Stubbing Behavior of Spies
Chapter 6: Verifying Test Doubles
Chapter 7: Verifying Behavior with Object Matchers
Chapter 8: Refactoring with Mockito
Chapter 9: Integration Testing with Mockito and DI Frameworks
Chapter 10: Mocking Libraries Comparison
Index
  • Chapter 1: Getting Started with Mockito
    • Introduction
    • Adding Mockito to a project's classpath
    • Getting started with Mockito for JUnit
    • Getting started with Mockito for TestNG
    • Mockito best practices – test behavior not implementation
    • Adding Mockito hints to exception messages (JUnit) (Experimental)
    • Adding additional Mockito warnings to your tests (JUnit) (Experimental)
  • Chapter 2: Creating Mocks
    • Introduction
    • Creating mocks in code
    • Creating mocks with annotations
    • Creating mocks with a different default answer
    • Creating mocks with different default answers with annotations
    • Creating mocks with custom configuration
    • Creating mocks of final classes with PowerMock
    • Creating mocks of enums with PowerMock
  • Chapter 3: Creating Spies and Partial Mocks
    • Introduction
    • Creating spies in code
    • Creating spies with custom configuration
    • Creating spies using annotations
    • Creating partial mocks
    • Creating partial mocks of final classes with delegatesTo()
    • Creating spies of final classes with PowerMock
  • Chapter 4: Stubbing Behavior of Mocks
    • Introduction
    • Using argument matchers for stubbing
    • Stubbing methods that return values
    • Stubbing methods so that they throw exceptions
    • Stubbing methods so that they return custom answers
    • Stubbing methods so that they call real methods
    • Stubbing void methods
    • Stubbing void methods so that they throw exceptions
    • Stubbing void methods so that they return custom answers
    • Stubbing void methods so that they call real methods
    • Stubbing final methods with PowerMock
    • Stubbing static methods with PowerMock
    • Stubbing object instantiation using PowerMock
  • Chapter 5: Stubbing Behavior of Spies
    • Introduction
    • Stubbing methods that return values
    • Stubbing methods so that they throw exceptions
    • Stubbing methods so that they return custom answers
    • Stubbing void methods
    • Stubbing void methods so that they throw exceptions
    • Stubbing void methods so that they return custom answers
    • Stubbing final methods with PowerMock
  • Chapter 6: Verifying Test Doubles
    • Introduction
    • Verifying the method invocation count with times()
    • Verifying the method invocation count with atLeast()
    • Verifying the method invocation count with atMost()
    • Verifying that interactions never happened
    • Verifying that interactions stopped happening
    • Verifying the order of interactions
    • Verifying interactions and ignoring stubbed methods
    • Verifying the method invocation within the specified time
  • Chapter 7: Verifying Behavior with Object Matchers
    • Introduction
    • Using Hamcrest matchers for assertions
    • Creating custom Hamcrest matchers
    • Using Hamcrest matchers for stubbing and verification
    • Using AssertJ for assertions
    • Creating custom AssertJ assertions
    • Capturing and asserting the argument
  • Chapter 8: Refactoring with Mockito
    • Introduction
    • Removing the problems with instance creation
    • Refactoring classes that do too much
    • Refactoring the classes that use the class casts
    • Refactoring the classes that use static methods
    • Refactoring the tests that use too many mocks
  • Chapter 9: Integration Testing with Mockito and DI Frameworks
    • Introduction
    • Injecting test doubles instead of beans using Spring's code configuration
    • Injecting test doubles instead of beans using Spring's XML configuration
    • Injecting test doubles instead of beans using Springockito
    • Injecting test doubles instead of beans with Guice
    • Injecting test doubles instead of beans with Guice using Jukito

Marcin Grzejszczak

Marcin Grzejszczak is an experienced Java programmer. He is enthusiastic about clean coding and good design. He has contributed to several open source projects (Drools, Moco, Mockito, Spock, and so on) and to Groovy core. He is the co-organizer of the Warsaw Groovy User Group. He is a member of the Most Valuable Blogger program at DZone and Java Code Geeks.

Marcin is the author of Instant Mockito, Packt Publishing, and Drools Refcard at DZone. You can visit his blog, http://toomuchcoding.blogspot.com, or his home page, http://www.marcin.grzejszczak.pl. Or, you can follow him on Twitter at http://twitter.com/MGrzejszczak.

Sorry, we don't have any reviews for this title yet.

Code Downloads

Download the code and support files for this book.


Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Mockito Cookbook +    Mastering Web Application Development with AngularJS =
50% Off
the second eBook
Price for both: $36.45

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Create beautiful tests using Mockito
  • Create mocks and spies in a number of ways
  • Implement best practices to perform tests with Mockito
  • Extend Mockito with other popular Java-based unit testing frameworks such as JUnit and PowerMock
  • Stub behavior of mocks and spies
  • Verify test doubles with Mockito
  • Write good tests using Mockito
  • Integrate Mockito with DI systems
  • Compare Mockito to other mocking frameworks
  • Verify the behavior of your system under test

In Detail

Mockito is a testing framework that allows you to easily create test doubles (mocks) of the system under test's collaborators. You can use mocks to better simulate the behavioral patterns of your software in isolation to receive feedback from tests as soon as possible.

This book consists of recipes that depict the usage of a vast majority of Mockito functionalities in real-life examples. It goes beyond the documentation and covers how Mockito works and shows the steps to write effective tests using Mockito. This is a focused guide with lots of practical recipes of business issues and presentation of the entire test of the system. This book shows the use of Mockito's popular unit testing frameworks such as JUnit, PowerMock, TestNG, and so on.

Approach

This is a focused guide with lots of practical recipes with presentations of business issues and presentation of the whole test of the system. This book shows the use of Mockito's popular unit testing frameworks such as JUnit, PowerMock, TestNG, and so on.

Who this book is for

If you are a software developer with no testing experience (especially with Mockito) and you want to start using Mockito in the most efficient way then this book is for you. This book assumes that you have a good knowledge level and understanding of Java-based unit testing frameworks.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software