Java Reference
In-Depth Information
•
assertTrue( boolean expression )
•
assertFalse( boolean expression )
•
assertNull (Object)
•
assertNotNull (Object)
•
assertSame (Object1, Object2)
•
assertNotSame (Object1, Object2)
•
fail()
Each of the assert methods comes in two “flavors,” one with a message
String
and one without. For example, there is a method
assertTrue()
which
takes a
boolean
as its parameter; typically it would be used with an expression,
for example:
1
assertTrue( (sample actual) );
If the condition is not true, an
AssertionFailedError
is thrown. That
means, among other things, that if/when your test fails, it will stop executing
at that point. The
tearDown()
method, though, will still be executed before
proceeding to the next test.
There is also a method of the same name,
assertTrue()
, but with a
slightly different signature—it adds a
String
as its first parameter. The string
is the message to be included in the error report. Using this variation on
assertTrue()
, our example would become:
assertTrue("Sample too small", (sample actual));
In the same way,
assertFalse()
has two versions—
assertFalse(boolean)
and
assertFalse(String, boolean)
—and so
on for all other assert methods.
1. Yes, the extra parentheses are not needed; they just make the point that this is a boolean ex-
pression being passed as the argument to
assertTrue()
. We could also have written it as:
boolean result = (sample actual);
assertTrue(result);
Again, the extra parentheses are used just to make it clearer.