Java Reference
In-Depth Information
testing a non-static method, it may be necessary to create objects that are to be
used in several tests. So these objects may have to be created before any of the
test procedures are called. To accomplish this, define this procedure in class
TestMax
:
protected void
setUp() { ... }
Procedure
setUp
is called before any of the procedures whose name begins with
test
are called.
Write one test procedure or many?
It is possible to include all the test cases in a single test procedure, as fol-
lows:
public void
testAllTestCases() {
assertEquals(7, SimpleMath.max(7, 5));
assertEquals(5, SimpleMath.max(-5, 5));
assertEquals(8, SimpleMath.max(8, 8));
}
The problem with doing this is that only one failure will be reported because a
test procedure terminates as soon as
one
failure is detected and reported. In gen-
eral, one wants to exercise as many test cases as possible. The guideline to fol-
low, then, is the following:
Guideline:
Exercise independent test cases in different test pro-
cedures.
/**
Display an error message if expected value
ob1
does not equal
ob2
(use
ob1.equals(ob2)
for the test)
*/
assertEquals(Object ob1, Object ob2)
/**
Display an error message that includes
s
if expected value
ob1
does not equal
ob2
(use
ob1.equals(ob2)
for the test)
*/
assertEquals(String s, Object ob1, Object ob2)
/**
Display a message if
p1 != p2
. (Methods exist for the other primitive types.)
*/
assertEquals(
int
p1,
int
p2)
/**
Display a message that includes
s
if
p1 != p
. (also for the other primitive types)
*/
assertEquals(String s,
int
p1,
int
p2)
/**
Display a message that includes
s
if
b
is not
true
.
*/
assertTrue(String s,
boolean
b)
/**
Display a message that includes
s
if
b
is not
false
.
*/
assertFalse(String s,
boolean
b)
Figure I.3:
Methods of class
TestCase
Search WWH ::
Custom Search