Java Reference
In-Depth Information
testCreateSub()
is a single JUnit test which asserts various conditions about
various invocations of the
createSub()
method. Note that all of the assertions
encountered in the execution of the test class must pass for the test to pass.
So what happens if an assertion fails? The assert method will throw an ex-
ception, reporting the failure. In JUnit terminology, a
failure
is a test that didn't
pass, whereas an
error
is a problem with the running of the test. A missing class
or a null pointer exception are errors, whereas an
assertNotNull()
call failing
is considered a test failure.
The handy thing about the exceptions that the assert methods throw is
that they are, technically speaking, not
java.lang.Exception
throwables but
rather belong to the
java.lang.Error
type of throwable. (Don't confuse this
technical Java use of the word “error” with our more informal use in the previ-
ous discussion of failure versus error.) To quote from the Javadoc page for
java.lang.Error
:
A method is not required to declare in its
throws
clause any subclasses of
Error
that might be thrown during the execution of the method but not
caught, since these errors are abnormal conditions that should never occur.
So the use of
Error
by JUnit's various assert methods is done simply as a
convenience for us test developers, so that we don't have to put
throws ...
clauses on all of our method declarations.
13.5.1
These are the various test assertions available with JUnit:
JUnit Assertions
•
assertEquals()
, comparing
•
boolean
with
boolean
•
char
with
char
•
short
with
short
•
int
with
int
•
long
with
long
•
float
with
float
•
double
with
double
•
Object
with
Object
•
String
with
String