Book Image

Test-Driven Java Development

Book Image

Test-Driven Java Development

Overview of this book

Table of Contents (17 chapters)
Test-Driven Java Development
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
8
Refactoring Legacy Code – Making it Young Again
Index

Unit testing with TDD


What is the difference in the way we write unit tests in the context of TDD? The major differentiator is in when. While traditionally unit tests are written after the implementation code is done, in TDD we write tests before—the order of things is inverted. Without TDD, the purpose of unit tests is to validate an existing code. TDD teaches us that unit tests should drive our development and design. They should define the behavior of the smallest possible unit. They are micro requirements pending to be developed. A test tells you what to do next and when you're done doing it. Depending on the type of tests (unit, functional, integration, and so on), the scope of what should be done next differs. In the case of TDD with unit tests, this scope is the smallest possible, meaning a method or, more often, a part of it. Moreover, with TDD driven with unit tests, we are forced to comply to some design principles such as KISS (keep it simple stupid). By writing simple tests with...