Instant Mockito

Book description

Learn how to create stubs, mocks, and spies, and verify their behaviour using Mockito

  • Learn something new in an Instant! A short, fast, focused guide delivering immediate results
  • Stub methods with callbacks
  • Verify the behavior of test mocks
  • Assert the arguments passed to functions of mocks
  • Create custom argument matchers

In Detail

Mockito is an open source testing framework for Java that automates unit tests for use in test-driven development (TDD) or behavior-driven development (BDD), decoupling test code from the system under test by eliminating the expect-run-verify pattern. It lets you write beautiful tests with a clean and simple API.

Instant Mockito contains plenty of real-life examples on how to unit test your code. It shows you how to use this testing framework and explains best practices on how to use it effectively.

Instant Mockito helps you break each significant method into a set of practical examples. It will clarify all the concepts of unit testing and Mockito.

You will learn how to properly capture and verify arguments passed into your stubbed methods. We will also take a look at verifying an order and returning custom answers on the stubs. If you want to take advantage of using the Mockito framework’s API to tame your code base, then this is the book for you.

You will learn everything you need to know to unit test your code in a clear, readable, and high quality manner.

Table of contents

  1. Instant Mockito
    1. Instant Mockito
    2. Credits
    3. About the Author
    4. About the Reviewer
      1. Support files, eBooks, discount offers and more
      1. Why Subscribe?
      2. Free Access for Packt account holders
    7. 1. Instant Mockito
      1. So, what is Mockito?
        1. Using Mockito
        2. Integrating Mockito within your existing websites/applications
      2. Installation
        1. Step 1 – what do I need?
        2. Step 2 – attaching JARs to the classpath
        3. And that's it
      3. Quick start – creating your first unit test with Mockito
        1. Step 1 – picking a class that you want to unit test
        2. Step 2 – picking a method that you want to unit test
        3. Step 3 – creating a unit test class with a properly defined method
        4. Step 4 – creating mocks and setting them in the object under testing
        5. Step 5 – defining, executing, and verifying mock behavior
      4. Top 8 features you need to know about
        1. Performing argument matching
        2. Stubbing multiple calls
        3. Working with void methods and thrown exceptions
        4. Stubbing with a custom answer
        5. Verifying behavior (including argument capturing, verifying call order, and working with asynchronous code)
        6. Doing partial mocking (spying)
        7. Reducing boilerplate code with annotations
          1. Removing boilerplate code by using the MockitoJUnitRunner
          2. Removing boilerplate code when you can't use the MockitoJUnitRunner
        8. Taking advantage of advanced mocks configuration
          1. Getting more information on NullPointerException
          2. Providing additional interfaces
      5. People and places you should get to know
        1. Official sites
        2. Articles and tutorials
        3. Community
        4. Blogs
        5. Twitter

Product information

  • Title: Instant Mockito
  • Author(s):
  • Release date: August 2013
  • Publisher(s): Packt Publishing
  • ISBN: 9781782167976