JUnit test cases are Java classes that contain one or more unit test methods, and these tests are grouped into test suites. You can run tests individually, or you can run entire test suites.
The below various assertXXX() methods that can be found in junit.framework.Assert. Although you could get by with using asertTrue() for nearly every test, using one of the more specific assertXXX() methods often makes your tests more understandable and provides good failure messages.
- assert( ) : This was deprecated in JUnit 3.7 because it interferes with the J2SE 1.4 assertkeyword. You should use assertTrue() instead. This method was completely removed in JUnit 3.8.
- assertEquals( ) : Compares two values for equality. The test passes if the values are equal.
- assertFalse( ) : Evaluates a boolean expression. The test passes if the expression is false.
- assertNotNull( ) : Compares an object reference to null. The test passes if the reference is not null.
- assertNotSame( ) : Compares the memory address of two object references using the == operator. The test passes if both refer to different objects.
- assertNull( ) : Compares an object reference to null. The test passes if the reference is null.
- assertSame( ) : Compares the memory address of two object references using the == operator. The test passes if both refer to the same object.
- assertTrue( ) : Evaluates a boolean expression. The test passes if the expression is true.
- fail( ) : Causes the current test to fail. This is commonly used with exception handling.
Here is an example that shows two assert statements, one with the description and one without:
- assertEquals(employeeA, employeeB);
- assertEquals("Employees should be equal after the clone() operation.", employeeA, employeeB);