Mockito Essentials

Book description

A practical guide to get you up and running with unit testing using Mockito

In Detail

Whether you are new to JUnit testing and mocking or a seasoned Mockito expert, this book will provide you with the skills you need to successfully build and maintain meaningful JUnit test cases and effectively mock external dependencies. At the beginning, this book deals with dummy and fake objects and then moves on to exploring stubs followed by an example on spying. Then, it will show you how to make legacy code testable, mock external dependencies using Mockito, and write testable code for greenfield projects. You will also gain an insight on the concepts of Service-oriented Architecture (SOA) as well as understand how to unit test RESTful web services with Mockito. By sequentially working through the steps in each chapter, you will quickly learn the features of Mockito.

Mockito Essentials will ensure your success with these concepts, tools, and frameworks.

What You Will Learn

  • Explore test doubles and work with dummy, spy, fake, stub, and mock objects
  • Uncover the Mockito architecture and build a custom mocking framework
  • Mock, stub, and spy external code dependencies using Mockito
  • Practice Behavior-driven Development (BDD) with Mockito
  • Make legacy code testable by mocking and spying dependencies
  • Mock GWT and web service dependencies using Mockito
  • Discover argument captors, inline stubbing, and resetting mock objects

Publisher resources

Download Example Code

Table of contents

  1. Mockito Essentials
    1. Table of Contents
    2. Mockito Essentials
    3. Credits
    4. About the Author
    5. About the Reviewers
      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. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Exploring Test Doubles
      1. Working with unit tests
      2. Understanding test doubles
      3. Using dummy objects
      4. Working with stubs
      5. Exploring a test spy
      6. Getting started with mock objects
      7. Implementing fake objects – simulators
      8. Summary
    9. 2. Socializing with Mockito
      1. Exploring Mockito
        1. Exploring unit test qualities
        2. Realizing the significance of Mockito
      2. Working with Mockito
        1. Adding a Mockito dependency
        2. Stubbing method calls
        3. Throwing exceptions
        4. Using argument matchers
          1. Working with wildcard matchers
          2. Working with a custom ArgumentMatcher class
            1. Comparison matchers – equalTo, is, and not
            2. Compound value matchers – either, both, anyOf, allOf, and not
        5. Verifying method calls
          1. Verifying zero and no-more interactions
        6. Answering method calls
      3. Understanding the Mockito architecture
      4. Summary
    10. 3. Accelerating Mockito
      1. Learning advanced Mockito features
        1. Working with void methods
        2. Throwing exceptions from void methods
        3. Working with void method callbacks
        4. Learning doCallRealMethod and doNothing
        5. Exploring doReturn
        6. Verifying arguments using ArgumentCaptor
          1. Working with generic collection arguments
          2. Working with variable arguments and arrays
        7. Verifying an invocation order
        8. Spying objects
        9. Exploring Mockito annotations
        10. Changing the default Mockito settings
        11. Resetting mock objects
        12. Working with inline stubbing
        13. Determining mock details
      2. Summary
    11. 4. Behavior-driven Development with Mockito
      1. Understanding the context of BDD
        1. Exploring the top-down strategy
        2. Exploring the bottom-up strategy
        3. Finding the gaps
      2. Exploring BDD
      3. Exercising BDD with Mockito
        1. The BDD syntax
      4. Summary
    12. 5. Unit Testing the Legacy Code with Mockito
      1. Understanding the legacy code
      2. Exploring testing impediments
      3. Working with PowerMock
        1. Stubbing static methods
        2. Suppressing static blocks
        3. Suppressing a superclass constructor
        4. Suppressing our own constructor
        5. Suppressing methods
        6. Stubbing private methods
        7. Stubbing final methods
        8. Mocking final classes
      4. Designing for testability with Mockito
        1. Identifying constructor issues
        2. Realizing initialization issues
        3. Working with private methods
        4. Working with final methods
        5. Exploring static method issues
        6. Working with final classes
        7. Learning new concerns
        8. Exploring static variables and blocks
      5. Summary
    13. 6. Developing SOA with Mockito
      1. Exploring Service-oriented Architecture (SOA)
      2. Working with web services
        1. Exploring JAX-WS with Eclipse
        2. Developing a RESTful web service
        3. Building a RESTful web service with Spring Framework
      3. Summary
    14. 7. Unit Testing GWT Code with Mockito
      1. Exploring Ajax and GWT
      2. Learning the MVP pattern
      3. Developing a GWT application using MVP
      4. Unit testing the GWT code
      5. Summary
    15. Index

Product information

  • Title: Mockito Essentials
  • Author(s): Sujoy Acharya
  • Release date: October 2014
  • Publisher(s): Packt Publishing
  • ISBN: 9781783983605