Assertions in depth
Assertions are methods that check for a condition that can be evaluated. If the condition is not met, the assertion method will throw an exception, thereby aborting the execution of the test.
The JUnit API includes the class Assert
. This is the base class of all the TestCase
classes that hold several assertion methods useful for writing tests. These inherited methods test for a variety of conditions and are overloaded to support different parameter types. They can be grouped together in the following different sets, depending on the condition checked, for example:
assertEquals
assertTrue
assertFalse
assertNull
assertNotNull
assertSame
assertNotSame
fail
The condition tested is pretty obvious and is easily identifiable by the method name. Perhaps the ones that deserve some attention are assertEquals()
and assertSame()
. The former, when used on objects, asserts that both objects passed as parameters are equally calling the objects' equals()
method. The latter asserts that both objects...