Page 166 -
P. 166

The tests use a few additional commands defined by JUnit to tell the framework whether
                          the unit test passes or fails:

                          assertEquals([String message], expected, actual [, tolerance])
                            Causes the unit test to fail if expected is not equal to actual. If tolerance is specified, the
                            equality for floating point numbers is calculated to that tolerance.
                          assertSame([String message], expected, actual)
                            Causes the unit test to fail if expected does not refer to the same object as actual.
                          assertTrue([String message], boolean condition)
                            Causes the unit test to fail if the Boolean condition evaluates to false.
                          assertFalse([String message], boolean condition)
                            Causes the unit test to fail if the Boolean condition evaluates to true.
                          assertNull([String message], java.lang.Object object)
                            Causes the unit test to fail if object is not null.
                          assertNotNull([String message], java.lang.Object object)
                            Causes the unit test to fail if object is null.
                          fail([String message])
                            Causes the unit test to fail immediately.

                          Each assertion can optionally be given a message. In that case, if the test fails, the message
                          is displayed in the test report generated by the framework. In JUnit, a test that completes
                          without failing is considered to have passed.

                          Every test in JUnit must be able to be run independently of every other test, and the tests
                          should be able to be run in any order. The individual tests are grouped together into a test
                          case. Each test is a method in a test case object, which inherits from junit.framework.
                          TestCase. (The above assert commands are inherited from this class.) Each test case can
                          optionally have a setUp( ) function, which sets up any objects or values required for the
                          tests, and a tearDown( ) function, which restores the environment to its condition before
                          the test was run.

                          Unit Testing Example

                          The examples in this section are the individual test methods from a test case object called
                          testFeeCalculation. There are many tests that would exercise the fee calculation function
                          shown in the Refactoring section above. This example shows six of them. All of them
                          require an instance of the FeeCalculation class, which is set up using this setUp( ) function:

                            public FeeCalculation feeCalculation;
                            public void setUp( ) {
                                feeCalculation = new FeeCalculation( );
                            }
                          The first test simply verifies that the function has performed its calculation and has gener-
                          ated the right result by comparing the output to a known value, which was calculated by
                          hand using a calculator:


                   158  CHAPTER SEVEN
   161   162   163   164   165   166   167   168   169   170   171