Test-Driven Development with Mockito

Book description

Learn how to apply Test Driven Development and the Mockito framework in real life projects, using realistic, hands-on examples

  • Start writing clean, high quality code to apply Design Patterns and principles
  • Add new features to your project by applying Test-first development- JUnit 4.0 and Mockito framework
  • Make legacy code testable and clean up technical debts

In Detail

The usual life cycle of code involves adding code, breaking an existing functionality, fixing that and breaking a new area! This fragility can be fixed using automated tests and Test Driven Development.

TDD’s test first approach expedites the development process and unit tests act as safety nets for code refactoring and help in maintaining and extending the code. This makes TDD highly beneficial for new projects.

This practical, hands-on guide provides you with a number of clear, step-by-step exercises that will help you to take advantage of the real power that is behind Test Driven Development and the Mockito framework. By using this book, you will gain the knowledge that you need to use the Mockito framework in your project.

This book explains the concept of Test Driven Development (TDD), including mocking and refactoring, as well as breaking down the mystery and confusion that surrounds the test first approach of TDD. It will take you through a number of clear, practical examples that will help you to take advantage of TDD with the Mockito framework, quickly and painlessly.

You will learn how to write unit tests, refactor code and remove code smells. We will also take a look at mock objects and learn to use Mockito framework to stub, mock, verify and spy objects for testability. You will also learn to write clean, maintainable, and extensible code using design principles and patterns.

If you want to take advantage of using Test Driven Development and learn about mocking frameworks, then this is the book for you. You will learn everything you need to know to apply Test Driven Development in a real life project, as well as how to refactor legacy code and write quality code using design patterns.

Table of contents

  1. Test-Driven Development with Mockito
    1. Table of Contents
    2. Test-Driven Development with Mockito
    3. Credits
    4. About the Author
    5. About the Reviewer
    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 Familiar with TDD
      1. Definition of test
      2. The big picture
      3. Refactoring
      4. Summary
    9. 2. Refactoring – Roll the Dice
      1. Refactoring
        1. Reasons behind refactoring
        2. Refactoring schedule
        3. When not to refactor
        4. Stopping refactoring
        5. Code smell
          1. Switch statements
          2. Duplicate code
          4. Long methods and parameter list
          5. Large classes (aka GOD object)
          6. Agent classes
          7. Lazy, dead classes and dead code
          8. Over engineering (speculative generality)
          9. Wrong inheritance (refused bequest)
      2. Summary
    10. 3. Applying TDD
      1. Understanding different test types
      2. Understanding TDD – a real-life example
        1. Definition
        2. Common healthcare vocabulary
        3. Procedure
        4. The service catalogue
        5. MRN
        6. Encounter
      3. Summary
    11. 4. Understanding the Difference between Inside-out and Outside-in
      1. Understanding outside-in
        1. An example of ATDD
        2. Understanding the advantages and disadvantages of outside-in
      2. Understanding inside-out
        1. Understanding the advantages and disadvantages of inside-out
      3. Summary
    12. 5. Test Doubles
      1. Categories of test doubles
        1. Dummy
        2. Stub
        3. Fake
        4. Mock
      2. Summary
    13. 6. Mockito Magic
      1. An overview of Mockito
        1. Why you should use Mockito
      2. Qualities of unit testing
      3. Drinking Mockito
      4. Verifying redundant invocation
        1. Rationale
      5. The argument matcher
        1. Rationale
        2. Why we need wildcard matchers
        3. The ArgumentMatcher class
      6. Throwing exceptions
      7. Consecutive calls
      8. Stubbing with callbacks – using the Answer class
      9. Spying objects
      10. Using doReturn()
      11. Working with Void methods
      12. Argument capture
      13. Summary
    14. 7. Leveraging the Mockito Framework in TDD
      1. Applying Mockito
        1. Customer requirements
        2. Building the application
      2. Summary
    15. 8. World of Patterns
      1. Characteristics of a bad design
      2. Design principles
      3. Design patterns
        1. Classification of patterns
          1. Replacing conditional logic with command
          2. Applying a command pattern
        2. Replacing conditional logic with strategy
          1. Strategy pattern
      4. Summary
    16. 9. TDD, Legacy Code, and Mockito
      1. What is legacy code?
      2. Problems with legacy code
      3. Diving into the legacy puzzle
      4. Refactoring legacy code
      5. The role of Mockito
      6. Summary
    17. A. TDD Tools and Frameworks
      1. Discovering Eclipse
      2. Useful keys for TDD and refactoring
      3. General settings
      4. JUnit 4.x
        1. Running the first unit test
        2. Exception handling
        3. The test suite
        4. Ignoring a test
        5. Asserting a value
      5. Summary
    18. B. Agile Practices
      1. Exploring continuous integration
      2. Exploring Jenkins
        1. Configuring Jenkins
          1. Adding a build job
          2. Source code management
          3. Build triggers
          4. Scripting
          5. Post-build actions
          6. Security
      3. Revealing Agile methodologies
        1. Working with the Scrum methodology
          1. Roles
          2. Meetings
          3. The story board
        2. Exploring the Kanban development process
      4. Summary
    19. Index

Product information

  • Title: Test-Driven Development with Mockito
  • Author(s):
  • Release date: November 2013
  • Publisher(s): Packt Publishing
  • ISBN: 9781783283293