Book Image

Test-Driven Development with Mockito

By : Sujoy Acharya
Book Image

Test-Driven Development with Mockito

By: Sujoy Acharya

Overview of this book

<p>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.<br /><br />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.<br /><br />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.<br /><br />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.<br /><br />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.<br /><br />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.</p>
Table of Contents (18 chapters)
Test-Driven Development with Mockito
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
2
Refactoring – Roll the Dice
7
Leveraging the Mockito Framework in TDD
Index

Preface

Test-Driven Development (TDD) is an evolutionary approach to development. It offers test-first development where the production code is written only to satisfy a test. The simple idea of writing a test first reduces the extra effort of writing unit tests after coding.

In Test-Driven Development, test doubles and mock objects are extensively used to mock out external dependencies. Mockito is an open source, unit-testing framework for Java; it allows for the creation, verification, and stubbing of a mock object.

The focus of the book is to provide the readers with comprehensive details on how effectively Test-Driven Development with Mockito can be used for software development. The book begins by giving us an overview of TDD and its implementation. The application of Mockito in TDD is explained in separate chapters. Each chapter provides hands-on examples and step-by-step instructions to develop and execute the code.

What this book covers

This book is about Test-Driven Development and the Mockito framework. Each chapter in this book provides hands-on examples, where we look at how to use TDD and various Mockito features in a step-by-step fashion in detail.

Chapter 1, Getting Familiar with TDD, provides an overview on Test-Driven Development, the definition of test, the big picture, and the first TDD example. By the end of this chapter, the reader will be able to understand the core concept of TDD.

Chapter 2, Refactoring – Roll the Dice, focuses on getting the reader quickly started with code refactoring and code smells. By the end of this chapter, the reader will be able to identify code smells and refactor the smells.

Chapter 3, Applying TDD, explains the life cycle of TDD and focuses on getting the reader quickly started with Test-Driven Development. By the end of this chapter, the reader will be able to follow the TDD life cycle and write test-first code.

Chapter 4, Understanding the Difference Between Inside-out and Outside-in, explains the commonly used techniques of TDD. By the end of this chapter, the reader will be able to understand the core concept of classical and mockist TDD.

Chapter 5, Test Doubles, illustrates the concept of test doubles. Dummy, Stub, Mock, and Fake doubles are explained in detail. By the end of this chapter, the reader will be able to understand the core concept of Test Doubles.

Chapter 6, Mockito Magic, explains the concept of mock objects using the Mockito framework and provides examples to help the reader understand Mockito APIs. By the end of this chapter, the reader will be able to use Mockito APIs and various features of Mockito.

Chapter 7, Leveraging the Mockito Framework in TDD, explains the advanced features of the Mockito framework, and illustrates usages of Mockito in Test-Driven Development. By the end of this chapter, the reader will be able to use TDD with Mockito.

Chapter 8, World of Patterns, covers the definition and characteristics of a good design, design principles, design patterns, and usages of pattern to refactor code. By the end of this chapter, the reader will be able to identify a bad design and apply the design principle and patterns to refactor the bad design.

Chapter 9, TDD, Legacy Code and Mockito, covers the definition and characteristics of legacy code and provides examples to refactor the legacy code and write unit tests using Mockito. By the end of this chapter, the reader will be able to write unit tests and refactor the legacy code.

Appendix A, TDD Tools and Frameworks, deals with TDD tools and frameworks. It explains the basics of Eclipse and the effective use of keyboard shortcuts to refactor the code and expedite its development; it also explains JUnit 4.0 basics, JUnit 4.0 unit tests, and annotations. By the end of this appendix, the reader will have good understanding of the JUnit 4.0 framework and will be able to smartly use Eclipse using keyboard shortcuts.

Appendix B, Agile Practices, deals with agile concepts and explains continuous integration, provides an example to set up Jenkins to accomplish CIT, and explains the Scrum and Kanban development concepts. By the end of this appendix, the reader will have good understanding of continuous integration and will be able to build an automation using Jenkins and agile development methodology concepts such as Scrum and Kanban.

What you need for this book

You will need the following software to be installed before running the examples:

Who this book is for

This book is for developers who want to develop software according to Test Driven Development using Mockito and to leverage various Mockito features. Developers don't need prior knowledge of TDD, Mockito, or JUnit.

It is ideal for developers who have some experience in Java application development as well as some basic knowledge of unit testing, but it covers the basic fundamentals of TDD and JUnit testing to get you acquainted with these concepts before you use them.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include other contexts through the use of the import directive."

A block of code is set as follows:

public class LoanManager {

   private final LoanCalculator loanCalculator;
   public LoanManager(){
     loanCalculator = new LoanCalculator();
   }

   public LoanManager(LoanCalculator dependency){
     loanCalculator = dependency;
   }
   
  public void calculateMaxLoan(Person person){
    loanCalculator.calculate(person);
//other code
  }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

@Test(expected=RuntimeException.class)
public void inventory_access_raises_Error() {

when(inventory.getItemsExpireInAMonth()).thenThrow(new RuntimeException("Database Access fail"));

bazar.issueDiscountForItemsExpireIn30Days(.30);
fail("Code should not reach here");
}

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Clicking on the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.