Book Image

Testing with JUnit

By : Leonard Przybylski, Frank Appel
Book Image

Testing with JUnit

By: Leonard Przybylski, Frank Appel

Overview of this book

Table of Contents (16 chapters)
Testing with JUnit
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The four phases of a unit test


Well-written unit tests follow a clean and consistent structure based on four exactly defined phases. In this section, we will learn about the purpose of each of those and delve into some subtleties of fixture setup and result verification.

Using a common test structure

 

"A tidy house, a tidy mind."

 
 --Old adage

The attentive reader may have wondered why our first test setFetchCount was segmented by the use of empty lines. To answer this question, let's have an in-depth look at each of these segments with the first listing of this section, showing the common structure of a unit test. There are minor refactorings and additional comments compared to the version in the last chapter to emphasize more on the separation.

Note

Note how the constant NEW_FETCH_COUNT replaces the literal value assigned previously to a local input variable. Literal numbers often tell little or nothing about their purpose because they emerge from nowhere like a kind of miracle. Hence, they...