Instant Mock Testing with PowerMock [Instant]

This title is available as an eBook only
Instant Mock Testing with PowerMock [Instant]
eBook: $14.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print & eBook also available on:
Learn in an Instant - Short, Fast, Focused
Table of Contents
Sample Chapters
  • 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

Book Details

Language : English
eBook : 82 pages
Release Date : October 2013
ISBN : 1783289953
ISBN 13 : 9781783289950
Author(s) : Deep Shah
Topics and Technologies : All Books, Instant, Open Source

Table of Contents

Instant Mock Testing with PowerMock
  • Instant Mock Testing with PowerMock
    • Saying Hello World! (Simple)
    • Getting and installing PowerMock (Simple)
    • Mocking static methods (Simple)
    • Verifying method invocation (Simple)
    • Mocking final classes or methods (Simple)
    • Mocking constructors (Medium)
    • Understanding argument matchers (Medium)
    • Understanding the Answer interface (Advanced)
    • Partial mocking with spies (Advanced)
    • Mocking private methods (Medium)
    • Breaking the encapsulation (Advanced)
    • Suppressing unwanted behavior (Advanced)

Deep Shah

Deep Shah has been writing software for over a decade. As a child, he was always fascinated by computers. As time passed, his fascination grew into passion, and he decided to take up Software Development as a career choice. Deep has a degree in Computer Science and is a big fan of open source software. Being a Software Developer at heart, he likes exploring new programming languages, tools, and frameworks. Deep strongly believes in writing unit tests. He thinks that any code (no matter when it’s written) that does not have unit tests is legacy code. Deep has served stints with companies such as Amazon, SpiderLogic, and ThoughtWorks. He speaks a number of languages including Java, C#, JavaScript, Scala, and Haskell. In his free time, Deep likes to see the world, go to cool places, and click lots of pictures.

Code Downloads

Download the code and support files for this book.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


- 2 submitted: last submission 26 Jun 2014

Errata Type: Code  |   Page number: 10

Point number 6, towards the end of the code


Instead of



it should be


Errata type: Code   |    Page number: 11

Point number 12, towards the end of the code


Instead of;


it should be


Sorry, there are currently no downloads available for this title.

Frequently bought together

Instant Mock Testing with PowerMock [Instant] +    Advanced Quantitative Finance with C++ =
50% Off
the second eBook
Price for both: $21.95

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Learn how to mock final classes and methods
  • Use argument matchers to write flexible tests
  • Invoke private methods and verify the internal state of a class
  • Suppress unwanted constructors, methods, fields, and static initializers
  • Set up mocking code once and use Mock Policies to avoid writing repetitive code

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.


Filled with practical, step-by-step instructions and clear explanations for the most important and useful tasks. A concise guide full of step-by-step recipes, to teach you how you can use PowerMock to unit test code.

Who this book is for

If you are a beginner who would like to learn how to use PowerMock, this is the perfect guide for you! As the book comprehensively covers both basic and advanced concepts, this guide is also suitable for those of you who have an intermediate knowledge of PowerMock.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software