Mockito Cookbook

Over 65 recipes to get you up and running with unit testing using Mockito.

Mockito Cookbook

Cookbook
Marcin Grzejszczak

Over 65 recipes to get you up and running with unit testing using Mockito.
$10.00
$43.99
RRP $26.99
RRP $43.99
eBook
Print + eBook
$12.99 p/month

Get Access

Get Unlimited Access to every Packt eBook and Video course

Enjoy full and instant access to over 3000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

+ Collection
Free Sample

Book Details

ISBN 139781783982745
Paperback284 pages

About This Book

  • 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

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.

Table of Contents

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
Chapter 10: Mocking Libraries Comparison
Introduction
Mockito versus EasyMock
Mockito versus JMockit
Mockito versus JMock
Mockito versus Spock

What You Will Learn

  • 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.

Authors

Table of Contents

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
Chapter 10: Mocking Libraries Comparison
Introduction
Mockito versus EasyMock
Mockito versus JMockit
Mockito versus JMock
Mockito versus Spock

Book Details

ISBN 139781783982745
Paperback284 pages
Read More