Test cases for JUnit are written as Java classes that extend the JUnit
framework. These classes have a number of methods, each of which tests a
particular function, or unit, of the code.
For example, for a simple
accounting product, one test might cover logging in with a username and
password. A second might check depositing an amount of money and ensure the
balance increased by the same amount. A third test might mimic withdrawing money
from an account. Generally, the more test cases you write, the more thorough the
test will be overall. You should test every functional area of a product: Only
by doing this will the tests be of real value in making sure that small changes
in one place do not have larger implications in another.
Creating a new test case is easy. This class must extend the TestCase class and
have a constructor that calls the constructor of the base class.
This class is called the test harness, and it�s where you write your
tests. Each time a test is called, JUnit will execute the setUp() method for you
to initialize any values you need. Next, it will call a test case and then call
tearDown() to undo the initialization and go on to the next test.
This simple test harness contains two tests, testBooleanTrue() and
testBooleanFalse(). Each test must be declared public and must make a call to
JUnit to inform JUnit of the status of the test. In this case, we force one test
to always succeed and the other to always fail.