Book description
Over 65 recipes to get you up and running with unit testing using Mockito
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.
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
Table of contents
-
Mockito Cookbook
- Table of Contents
- Mockito Cookbook
- Credits
- About the Author
- About the Reviewers
- www.PacktPub.com
- Preface
-
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)
-
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
- 3. Creating Spies and Partial Mocks
-
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
-
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
-
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
- 7. Verifying Behavior with Object Matchers
- 8. Refactoring with Mockito
-
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
- 10. Mocking Libraries Comparison
- Index
Product information
- Title: Mockito Cookbook
- Author(s):
- Release date: June 2014
- Publisher(s): Packt Publishing
- ISBN: 9781783982745
You might also like
book
Mockito Made Clear
Mockito is the most popular framework in the Java world for automating unit testing with dependencies. …
book
Apache Maven Cookbook
Over 90 hands-on recipes to successfully build and automate development life cycle tasks following Maven conventions …
video
Mockito Tutorial: Learn mocking with 25 Junit Examples
Writing great unit tests distinguishes good programmers from great programmers. In this course, you will learn …
book
Reactive Systems in Java
Reactive systems and event-driven architecture are becoming indispensable to application design, and companies are taking note. …