How to use assertEquals method in junit java, is it safe to use assertEquals method for checking object equality?


In this JUnit Tutorial in java we will learn how to use assertEquals method in junit java with example and programs, is it safe to use assertEquals method for checking object equality in java junit and how assertEqual method works internally in java.


Use assertEquals method in junit java >

assertEquals(string, string)  - Method compares the two objects for equality.


How assertEquals method works internally in junit java >
assertEquals(string, string) method internally uses the equals() method to determine equality of the passed objects in the method.

So, internally assertEquals() methods checks for str1.equals(str2)

Must know :
By default equals method checks for referential equality (Class may override the method and provide different functionality) in java.

String class overrides equals method of Object class and compares one string object to the other string object. The result is true if characters in both String object appears in same order in java.




What assertEquals method does not do junit java >
assertEquals() methods does not performs equality test by using  == operator (which checks for referential equality of object, it checks whether two references are referring to same object or not).


Program to show assertEquals method is safe for checking object equality in junit java>
String str1 and String str2 will be referring to two different string objects, but assertEquals(str1, str2) will return true because assertEquals internally uses equals method and equals method returns true if characters in both String object appears in same order in java.
import static org.junit.Assert.assertEquals;
public class AssertionsEqualExample {
   public static void main(String args[]) {
      String str1 = new String("ab");
      String str2 = new String("ab");
      assertEquals(str1, str2);
      System.out.println("after assertEquals");     
   }
}
/*OUTPUT
after assertEquals
*/

Jar used in above program > junit-4.8.2.jar


Program to show org.junit.ComparisonFailure is thrown - when equality test fails in java junit>
String str1 and String str2 will be referring to two different string objects, assertEquals(str1, str2) will return false and throws org.junit.ComparisonFailure because assertEquals internally uses equals method and equals method returns false if characters in both String object doesn’t appears in same order.
import static org.junit.Assert.assertEquals;
public class AssertionsEqualExceptionExample {
   public static void main(String args[]) {
      String str1 = new String("ab");
      String str2 = new String("xy");
      assertEquals(str1, str2); //org.junit.ComparisonFailure: expected:<[ab]> but was:<[xy]>
      System.out.println("after assertEquals");     
   }
}
/*OUTPUT
Exception in thread "main" org.junit.ComparisonFailure: expected:<[ab]> but was:<[xy]>
   at org.junit.Assert.assertEquals(Assert.java:123)
   at org.junit.Assert.assertEquals(Assert.java:145)
   at AssertionsEqualExceptionExample.main(AssertionsEqualExceptionExample.java:8)
*/


So in this JUnit Tutorial in java we learned how to use assertEquals method in junit java with example and programs, is it safe to use assertEquals method for checking object equality in java junit and how assertEqual method works internally in java.


RELATED LINKS>

Difference between equals method and == operator in java - testing with String and StringBuffer

Differences between throw and throws in java


Labels: Core Java JUNIT
eEdit
Must read for you :