JUnit various assertXXX( ) methods



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. 
  1. 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. 
  2. assertEquals( ) : Compares two values for equality. The test passes if the values are equal. 
  3. assertFalse( ) :  Evaluates a boolean expression. The test passes if the expression is false. 
  4. assertNotNull( ) : Compares an object reference to null. The test passes if the reference is not null. 
  5. assertNotSame( ) : Compares the memory address of two object references using the == operator. The test passes if both  refer to different objects. 
  6. assertNull( )  :  Compares an object reference to null. The test passes if the reference is null. 
  7. assertSame( ) : Compares the memory address of two object references using the == operator. The test passes if both refer to the same object. 
  8. assertTrue( )   : Evaluates a boolean expression. The test passes if the expression is true. 
  9. 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); 




Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email