Chapter 2. Elementary tests
This chapter covers
- General techniques for testing individual methods
- Specific techniques for testing JavaBeans
- Specific techniques for testing interfaces
- Comparing objects for equality in tests
The simplest kind of Programmer Test you will write verifies the return value of a method. This technique is the building block for all the testing you will ever do with JUnit. In the rest of this book we try to reduce every complex testing problem down to this, the simplest of scenarios. We begin by describing this scenario in detail; then the remaining recipes in this chapter will build gradually from there. These recipes provide not only the solutions to common problems, but also a rudimentary vocabulary that we can use to describe the solutions to larger, more complex testing problems. If verifying a method’s return value is the atom of unit testing, then these recipes are the lightest molecules: the ones that come first and the ones from which complex organic compounds are built. By the end of this book you will have strategies for handling most of, if not all, the testing problems you encounter.
Here is how to test a method that returns a value: invoke the method and compare its return value against the value you expect: