assertEquals() vs. assertSame() in JUnit
JUnit is a widely used testing framework. Its API offers a straightforward approach to checking and comparing objects. However, the distinction between the assertEquals()
and assertSame()
methods is not always clear. Let us delve into understanding the differences between assertEquals()
and assertSame()
in Java testing frameworks, particularly JUnit.
1. Introduction
In Java, understanding the concepts of identity and equality is crucial for writing effective tests. Let’s explore these concepts and how they are tested using JUnit4 and JUnit5.
1.1 Identity
Identity refers to the memory address of an object. Two object references are identical if they point to the same memory location. This is tested using the ==
operator.
Code snippet
Object a = new Object(); Object b = a; // a and b reference the same object Object c = new Object(); // c references a different object System.out.println(a == b); // true System.out.println(a == c); // false
1.2 Equality
Equality refers to the logical comparison of two objects, which is usually implemented by overriding the equals()
method. Two objects are equal if their states are the same, even if they are different instances.
Code snippet
class Person { String name; Person(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(name, person.name); } } Person p1 = new Person("John"); Person p2 = new Person("John"); Person p3 = new Person("Jane"); System.out.println(p1.equals(p2)); // true System.out.println(p1.equals(p3)); // false
2. JUnit4 and JUnit5 Examples
Let us delve into understanding the differences between assertEquals
and assertSame
in Java testing frameworks, particularly JUnit.
assertEquals
is used to check if two objects are “equal” in terms of their values. This method relies on the.equals()
method of the objects being compared. If the objects are of a primitive data type,assertEquals
will compare their actual values. For objects,assertEquals
will check if the objects are logically equivalent as defined by their.equals()
method.assertSame
is used to check if two references point to the same object. It does not consider the values of the objects but rather their reference identity. This means thatassertSame
will return true only if both references point to the same object in memory.
2.1 JUnit 4
In JUnit4, you use assertions from the org.junit.Assert
class to test identity and equality.
import static org.junit.Assert.*; import org.junit.Test; public class IdentityEqualityTestJUnit4 { @Test public void testIdentity() { Object a = new Object(); Object b = a; Object c = new Object(); assertTrue(a == b); // Passes assertFalse(a == c); // Passes } @Test public void testEquality() { Person p1 = new Person("John"); Person p2 = new Person("John"); Person p3 = new Person("Jane"); assertEquals(p1, p2); // Passes assertNotEquals(p1, p3); // Passes } }
2.2 JUnit 5
In JUnit5, you use assertions from the org.junit.jupiter.api.Assertions
class.
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class IdentityEqualityTestJUnit5 { @Test public void testIdentity() { Object a = new Object(); Object b = a; Object c = new Object(); assertTrue(a == b); // Passes assertFalse(a == c); // Passes } @Test public void testEquality() { Person p1 = new Person("John"); Person p2 = new Person("John"); Person p3 = new Person("Jane"); assertEquals(p1, p2); // Passes assertNotEquals(p1, p3); // Passes } }
2.3 Comparison
Key Differences | assertEquals | assertSame |
---|---|---|
Comparison Basis | Compares values using the .equals() method. | Compares references to ensure they point to the same object in memory. |
Use Cases | Use assertEquals when you need to verify that two objects have the same value. | Use assertSame when you need to verify that two references point to the same object. |
3. Conclusion
Understanding the difference between identity and equality is essential for writing accurate tests. In Java, the ==
operator checks for identity, while the equals()
method checks for equality. JUnit4 and JUnit5 provide assertions to facilitate testing these concepts effectively.