You are previewing Mockito Cookbook.
O'Reilly logo
Mockito Cookbook

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
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Mockito Cookbook
      1. Table of Contents
      2. Mockito Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with Mockito
        1. Introduction
        2. Adding Mockito to a project's classpath
          1. How to do it...
          2. See also
        3. Getting started with Mockito for JUnit
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Getting started with Mockito for TestNG
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Mockito best practices – test behavior not implementation
          1. Getting ready
          2. How to do it...
          3. See also
        6. Adding Mockito hints to exception messages (JUnit) (Experimental)
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Adding additional Mockito warnings to your tests (JUnit) (Experimental)
          1. How to do it...
          2. How it works...
      9. 2. Creating Mocks
        1. Introduction
        2. Creating mocks in code
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Creating mocks with annotations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Creating mocks with a different default answer
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Creating mocks with different default answers with annotations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Creating mocks with custom configuration
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Creating mocks of final classes with PowerMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Creating mocks of enums with PowerMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      10. 3. Creating Spies and Partial Mocks
        1. Introduction
        2. Creating spies in code
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Creating spies with custom configuration
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Creating spies using annotations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Creating partial mocks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Creating partial mocks of final classes with delegatesTo()
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Creating spies of final classes with PowerMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      11. 4. Stubbing Behavior of Mocks
        1. Introduction
        2. Using argument matchers for stubbing
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Stubbing methods that return values
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Stubbing methods so that they throw exceptions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Stubbing methods so that they return custom answers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Stubbing methods so that they call real methods
          1. Getting ready
          2. How to do it...
          3. See also
        7. Stubbing void methods
          1. How to do it...
          2. How it works...
          3. See also
        8. Stubbing void methods so that they throw exceptions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        9. Stubbing void methods so that they return custom answers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        10. Stubbing void methods so that they call real methods
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        11. Stubbing final methods with PowerMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        12. Stubbing static methods with PowerMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        13. Stubbing object instantiation using PowerMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      12. 5. Stubbing Behavior of Spies
        1. Introduction
        2. Stubbing methods that return values
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Stubbing methods so that they throw exceptions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Stubbing methods so that they return custom answers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Stubbing void methods
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Stubbing void methods so that they throw exceptions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Stubbing void methods so that they return custom answers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Stubbing final methods with PowerMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      13. 6. Verifying Test Doubles
        1. Introduction
        2. Verifying the method invocation count with times()
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Verifying the method invocation count with atLeast()
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Verifying the method invocation count with atMost()
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Verifying that interactions never happened
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Verifying that interactions stopped happening
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Verifying the order of interactions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Verifying interactions and ignoring stubbed methods
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. See also
        9. Verifying the method invocation within the specified time
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. There's more...
          5. See also
      14. 7. Verifying Behavior with Object Matchers
        1. Introduction
        2. Using Hamcrest matchers for assertions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Creating custom Hamcrest matchers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Using Hamcrest matchers for stubbing and verification
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Using AssertJ for assertions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Creating custom AssertJ assertions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Capturing and asserting the argument
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      15. 8. Refactoring with Mockito
        1. Introduction
        2. Removing the problems with instance creation
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Refactoring classes that do too much
          1. Getting ready
          2. How to do it...
          3. See also
        4. Refactoring the classes that use the class casts
          1. Getting ready
          2. How to do it...
          3. See also
        5. Refactoring the classes that use static methods
          1. Getting ready
          2. How to do it...
          3. See also
        6. Refactoring the tests that use too many mocks
          1. Getting ready
          2. How to do it...
          3. How it works..
          4. There's more…
          5. See also
      16. 9. Integration Testing with Mockito and DI Frameworks
        1. Introduction
        2. Injecting test doubles instead of beans using Spring's code configuration
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Injecting test doubles instead of beans using Spring's XML configuration
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Injecting test doubles instead of beans using Springockito
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Injecting test doubles instead of beans with Guice
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Injecting test doubles instead of beans with Guice using Jukito
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      17. 10. Mocking Libraries Comparison
        1. Introduction
        2. Mockito versus EasyMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Mockito versus JMockit
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Mockito versus JMock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Mockito versus Spock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      18. Index