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

Chapter 6. Mocking – Removing External Dependencies

 

"Talk is cheap. Show me the code."

 
 --Linus Torvalds

TDD is about speed. We want to quickly demonstrate whether some idea, concept, or implementation, is valid or not. Further on, we want to run all tests fast. A major bottleneck to this speed is external dependencies. Setting up DB data required by tests can be time-consuming. The execution of tests that verify code that uses third-party API can be slow. Most importantly, writing tests that satisfy all external dependencies can become too complicated to be worthwhile. Mocking both external and internal dependencies helps us solve these problems.

We'll build on what we did in Chapter 3, Red-Green-Refactor - From Failure Through Success Until Perfection. We'll extend Tic-Tac-Toe to use MongoDB as data storage. None of our unit tests will actually use MongoDB since all communications will be mocked. At the end, we'll create an integration test that will verify that our code and MongoDB are...