The Assert class provides convenient means for condition testing.
Class methods in the Assert class can be used to verify certain
conditions and to raise exceptions if the conditions are not met.
Assertions are particularly useful during the development of a
project and may be turned off in production code. Turning off
exceptions causes Assert not to raise exceptions when conditions
are not met.
Typical usage:
Assert.assert(Assert.enabled && mytest(), "my test failed");
Such usage prevents mytest() from being executed if assertions
are disabled. This techinique allows assertions to do time-consuming
checks (such as myArray.containsObject(myObject)) without
worrying about them impacting the performance of the final release.
If you know that the condition being tested is fast, you may omit the
enabled flag:
Assert.assert(myValue != null);
Note that even in this second usage, if assertions are disabled,
the assert() method will do nothing.
Another usage that is more efficient but bulkier:
if (Assert.enabled && Assert.assert(mytest(), "my test failed"));
Such usage prevents not only mytest() from being executed if
assertions are disabled but also the assert method itself, and some
compilers can remove the entire statement if assertions are disabled.
Assert is intended for general condition and invariant testing;
for parameter testing, use ParameterCheck.
enabled
True if failed assertions should raise exceptions, or false if they
should do nothing.
Method Summary
static boolean
assert(boolean test)
Throws AssertionFailureException if enabled is true and
test is false; otherwise, does nothing.
static boolean
assert(boolean test,
java.lang.String message)
Throws AssertionFailureException with a message of message
if enabled is true and test is false; otherwise, does nothing.
static void
setEnabled(boolean newEnabled)
Sets enabled to newEnabled.