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.
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.
You will need the following software to be installed before running the examples:
Java 5 or higher. JDK 1.5 or higher can be downloaded from the Oracle site: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase5-419410.html.
An Eclipse editor. The latest version of Eclipse is Kepler (4.3). Kepler can be downloaded from the following site http://www.eclipse.org/downloads/.
Mockito is required for the creation and verification of mock objects, and for stubbing. Mockito can be downloaded from https://code.google.com/p/mockito/downloads/list.
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.
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".
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 <[email protected]>
, 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.
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.
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.
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 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 <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.