You are previewing Instant Mock Testing with PowerMock.
O'Reilly logo
Instant Mock Testing with PowerMock

Book Description

Discover unit testing using PowerMock

  • Learn something new in an Instant! A short, fast, focused guide delivering immediate results

  • Understand how to test unit code using PowerMock, through hands-on-examples

  • Learn how to avoid unwanted behaviour of code using PowerMock for testing

  • Explore the features of PowerMock through the selection of clear, practical, step-by-step recipes

In Detail

Writing good unit tests can be hard in situations where we have to deal with static, final, or private methods. Sometimes, even a good design is sacrificed for the sake of testability. A mocking framework’s ability to create mocks for almost any scenario enables us to focus on accurately programming for a business environment.

Starting with a basic example of how to create a mock and verify a method invocation Instant Testing with PowerMock then demonstrates various features of PowerMock using clear, practical, step-by-step recipes. By the end of this book, you will be able to write unit tests for almost any scenario and suppress unwanted behaviour to effectively unit test your code.

Moving on from the basics, you will learn to write unit tests for static, final, and private methods, and write flexible unit tests using argument matchers. Following on from this, you will also learn how to mock a few methods of a class, and invoke real implementation of others using partial mocks or spies. This book will teach you the tricks of the trade that will enable you to write good unit tests for a wide range of scenarios.

Table of Contents

  1. Instant Mock Testing with PowerMock
    1. Instant Mock Testing with PowerMock
    2. Credits
    3. About the Author
    4. About the reviewer
    5. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    6. 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
    7. 1. Instant Mock Testing with PowerMock
      1. Saying Hello World! (Simple)
        1. How to do it...
        2. How it works...
        3. There's more...
          1. MockSettings
      2. Getting and installing PowerMock (Simple)
        1. Getting ready
        2. How to do it...
        3. There's more...
          1. Maven integration
          2. Other integration options
      3. Mocking static methods (Simple)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. .doNothing and .doThrow on instance methods
      4. Verifying method invocation (Simple)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Other verification modes
          2. InOrder verification
      5. Mocking final classes or methods (Simple)
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Mocking constructors (Medium)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      7. Understanding argument matchers (Medium)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Other built-in argument matchers
      8. Understanding the Answer interface (Advanced)
        1. Getting ready
        2. How to do it...
        3. How it works...
          1. There's more...
            1. PowerMockito.mock with default Answer
        4. Partial mocking with spies (Advanced)
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Mocking private methods (Medium)
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Alternate way of mocking private methods
        6. Breaking the encapsulation (Advanced)
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Suppressing unwanted behavior (Advanced)
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Suppressing constructors
            2. Suppressing fields