Java Reference
In-Depth Information
put a mistake in the function body.
Also in Fig. I.2 is a class
TestMax
, assumed to be in file
TestMax.java
. This
class contains three test cases to be used in testing method
max
. This class has the
following properties:
1. It imports
junit.framework.TestCase
.
2. It extends class
TestCase
.
3. It contains three public procedures without parameters, all of whose
names begin with
test
. We call these
test procedures
.
4. Each test procedure tests one test case by calling
assertEquals
. The first
argument of the call is the result that is expected from the test case; the
second argument, a call to method
max
that is the test case.
Suppose the two classes of Fig. I.2 are in the same directory and have been
opened in DrJava and compiled. Finally, suppose that file
TestMax
is selected in
DrJava and thus appears in the file window. Then, clicking button
Test
in the
righthand part of the tool bar at the top of DrJava causes calls to the three test
procedures to be executed. Each procedure contains a call to procedure
assert-
Equals
. If the first argument of that call equals the second, nothing is printed.
But if the first argument of that call does not equal the second, an error message
is printed in the
Test output
pane of the lower window.
In the case of the classes of Fig. I.2, these error messages will appear in the
Test output
pane:
2 tests failed:
File: .../testjunit/TestMax.java [line: 8]
Error: expected:<5> but was:<4>
File: .../testjunit/TestMax.java [line: 12]
Error: expected:<8> but was:<7>
Click the mouse button on one of these error messages to highlight the line
in file
TestMax
that produced the error message, so you can easily see which test
case failed.
Conventions for test procedures
When button
test
in the tools bar is clicked, each test procedure is called
once to exercise a test case. In class
TestMax
of Fig. I.2, each test procedure sim-
ply contains a call of
assertEquals
to perform such a test. However, the test
procedure can do anything. It can have assignments, if-statements, loops —
whatever is necessary to exercise the test case.
Figure I.3 describes other inherited procedures (besides
assertEquals
) that
could be used to exercise test cases.
Class
TestMax
of Fig. I.2 tests a rather simple static method. When testing
more complicated ones that interact in some fashion with other methods, or when
Search WWH ::
Custom Search