Mockito Cookbook
eBook - ePub

Mockito Cookbook

  1. 284 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mockito Cookbook

About this book

In Detail

Mockito is a testing framework that creates external dependencies (which is called mocking) which is used to test/simulate the behavioral patterns of the software and generate reports about it. Frameworks like Mockito fake or mock the external dependencies so that the object being tested has a consistent interaction with the outside dependencies. Mockito streamlines the delivery of the external dependencies that are not subjects of the test. Why Mockito? Compared to its main Java competitors EasyMock and jMock, Mockito, it is the most widely used mocking framework.

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. It begins with getting to know the Mockito configuration, then you will learn how to create mocks and stubs, and afterwards you will be shown how to write good tests using Mockito together with how to properly stub behavior of your test objects. We'll finish by showing you some of the most interesting Mockito extension-based tools together with a comparison of the most widely used mocking frameworks.

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.

Trusted by 375,005 students

Access to over 1 million titles for a fair monthly price.

Study more efficiently using our study tools.

Information

Year
2014
Edition
1
eBook ISBN
9781783982745

Mockito Cookbook


Table of Contents

Mockito Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started with Mockito
Introduction
Adding Mockito to a project's classpath
How to do it...
See also
Getting started with Mockito for JUnit
Getting ready
How to do it...
How it works...
There's more...
See also
Getting started with Mockito for TestNG
Getting ready
How to do it...
How it works...
See also
Mockito best practices – test behavior not implementation
Getting ready
How to do it...
See also
Adding Mockito hints to exception messages (JUnit) (Experimental)
Getting ready
How to do it...
How it works...
Adding additional Mockito warnings to your tests (JUnit) (Experimental)
How to do it...
How it works...
2. Creating Mocks
Introduction
Creating mocks in code
Getting ready
How to do it...
How it works...
See also
Creating mocks with annotations
Getting ready
How to do it...
How it works...
See also
Creating mocks with a different default answer
Getting ready
How to do it...
How it works...
There's more...
See also
Creating mocks with different default answers with annotations
Getting ready
How to do it...
How it works...
See also
Creating mocks with custom configuration
Getting ready
How to do it...
How it works...
There's more...
See also
Creating mocks of final classes with PowerMock
Getting ready
How to do it...
How it works...
See also
Creating mocks of enums with PowerMock
Getting ready
How to do it...
How it works...
See also
3. Creating Spies and Partial Mocks
Introduction
Creating spies in code
Getting ready
How to do it...
How it works...
See also
Creating spies with custom configuration
Getting ready
How to do it...
How it works...
See also
Creating spies using annotations
Getting ready
How to do it...
How it works...
There's more...
See also
Creating partial mocks
Getting ready
How to do it...
How it works...
There's more...
See also
Creating partial mocks of final classes with delegatesTo()
Getting ready
How to do it...
How it works...
There's more...
See also
Creating spies of final classes with PowerMock
Getting ready
How to do it...
How it works...
See also
4. Stubbing Behavior of Mocks
Introduction
Using argument matchers for stubbing
Getting ready
How to do it...
How it works...
There's more...
See also
Stubbing methods that return values
Getting ready
How to do it...
How it works...
There's more...
See also
Stubbing methods so that they throw exceptions
Getting ready
How to do it...
How it works...
There's more...
See also
Stubbing methods so that they return custom answers
Getting ready
How to do it...
How it works...
There's more...
See also
Stubbing methods so that they call real methods
Getting ready
How to do it...
See also
Stubbing void methods
How to do it...
How it works...
See also
Stubbing void methods so that they throw exceptions
Getting ready
How to do it...
How it works...
See also
Stubbing void methods so that they return custom answers
Getting ready
How to do it...
How it works...
See also
Stubbing void methods so that they call real methods
Getting ready
How to do it...
How it works...
See also
Stubbing final methods with PowerMock
Getting ready
How to do it...
How it works...
See also
Stubbing static methods with PowerMock
Getting ready
How to do it...
How it works...
See also
Stubbing object instantiation using PowerMock
Getting ready
How to do it...
How it works...
See also
5. Stubbing Behavior of Spies
Introduction
Stubbing methods that return values
Getting ready
How to do it...
How it works...
There's more...
See also
Stubbing methods so that they throw exceptions
Getting ready
How to do it...
How it works...
There's more...
See also
Stubbing methods so that they return custom answers
Getting ready
How to do it...
How it works...
There's more...
See also
Stubbing void methods
Getting ready
How to do it...
How it works...
There's more...
See also
Stubbing void methods so that they throw exceptions
Getting ready
How to do it...
How it works...
See also
Stubbing void methods so that they return custom answers
Getting ready
How to do it...
How it works...
See also
Stubbing final methods with PowerMock
Getting ready
How to do it...
How it works...
See also
6. Verifying Test Doubles
Introduction
Verifying the method invocation count with times()
Getting ready
How to do it...
How it works...
There's more...
See also
Verifying the method invocation count with atLeast()
Getting ready
How to do it...
How it works...
There's more...
See also
Verifying the method invocation count with atMost()
Getting ready
How to do it...
How it works...
See also
Verifying that interactions never happened
Getting ready
How to do it...
How it works...
There's more...
See also
Verifying that interactions stopped happening
Getting ready
How to do it...
How it works...
There's more...
See also
Verifying the order of interactions
Getting ready
How to do it...
How it works...
There's more...
See also
Verifying interactions and ignoring stubbed methods
Getting ready
How to do it…
How it works...
See also
Verifying the method invocation within the specified time
Getting ready
How to do it…
How it works...
There's more...
See also
7. Verifying Behavior with Object Matchers
Introduction
Using Hamcrest matchers for assertions
Getting ready
How to do it...
How it works...
There's more...
See also
Creating custom Hamcrest matchers
Getting ready
How to do it...
How it works...
There's more...
See also
Using Hamcrest matchers for stubbing and verification
Getting ready
How to do it...
How it works...
There's more...
See also
Using AssertJ for assertions
Getting ready
How to do it...
How it works...
There's more...
See also
Creating custom AssertJ assertions
Getting ready
How to do it...
How it works...
There's more...
See also
Capturing and asserting the argument
Getting ready
How to do it...
How it works...
See also
8. Refactoring ...

Table of contents

  1. Mockito Cookbook

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Mockito Cookbook by Marcin Grzejszczak in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Science General. We have over one million books available in our catalogue for you to explore.