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

Using test helpers


Besides components, collaborators, data types, and test cases, we occasionally need another category of classes to write our tests efficiently. These utility classes provide any kind of testing related functionality that we want to reuse in several tests. This section explains some of the common practices.

Motivation

While writing tests, we'll inevitably find ourselves in a situation were we'll code the same routine for fixture setup, verification, or the like. If this happens to be in the same test case, we can extract a method for common usage. But sometimes, we could also make use of these methods in other test classes.

Java provides class inheritance, and so it's possible to introduce a common super type supplying these helping methods. After that, a particular test case can extend from this type and reuse the testing related functionality. Problem solved, right?

Wrong! First of all, inheritance is more than what the compiler checks. Class hierarchies represent abstract...