We use assertSame and assertNotSame to check if two things are exactly the same object or not. This helps us make sure our program uses the right objects.
When you want to check if two variables point to the exact same object in memory.
When you want to confirm that two objects are not the same instance, even if they look similar.
When testing if a method returns the same object it was given or a new one.
When you want to avoid accidental duplication of objects in your code.
When verifying singleton patterns where only one instance should exist.
assertSame checks if both references point to the same object.
assertNotSame checks if both references point to different objects.
Examples
Passes if obj1 and obj2 are the same object.
JUnit
assertSame(obj1, obj2);
Passes if obj1 and obj3 are different objects.
JUnit
assertNotSame(obj1, obj3);
Passes because both are null, which counts as the same.
JUnit
assertSame(null, null);
Passes if obj1 is not null.
JUnit
assertNotSame(obj1, null);
Sample Program
This test class shows four tests:
testSameObjects passes because both variables point to the same string object.
testDifferentObjects passes because two different string objects with the same content are not the same object.
testFailSame fails because it expects two different objects to be the same.
testFailNotSame fails because it expects two references to the same object to be different.
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class AssertSameTest {
@Test
void testSameObjects() {
String a = "hello";
String b = a;
assertSame(a, b); // should pass
}
@Test
void testDifferentObjects() {
String a = new String("hello");
String b = new String("hello");
assertNotSame(a, b); // should pass
}
@Test
void testFailSame() {
String a = new String("test");
String b = new String("test");
assertSame(a, b); // should fail
}
@Test
void testFailNotSame() {
String a = "world";
String b = a;
assertNotSame(a, b); // should fail
}
}
OutputSuccess
Important Notes
Remember, assertSame checks object identity, not equality of content.
Strings created with new String() are different objects even if they have the same text.
Using assertSame with null values passes only if both are null.
Summary
assertSame checks if two references point to the exact same object.
assertNotSame checks if two references point to different objects.
Use these to test object identity, not just if objects look equal.
Practice
(1/5)
1. What does assertSame check in a JUnit test?
easy
A. If two references point to the exact same object
B. If two objects have the same content
C. If two objects are of the same class
D. If two references are both null
Solution
Step 1: Understand the purpose of assertSame
assertSame checks if both references point to the exact same object in memory, not just equal content.
Step 2: Differentiate from equality checks
Other assertions like assertEquals check if objects are equal in content, but assertSame checks identity.
Final Answer:
If two references point to the exact same object -> Option A
Quick Check:
assertSame = same object reference [OK]
Hint: assertSame checks object identity, not content equality [OK]
Common Mistakes:
Confusing assertSame with assertEquals
Thinking assertSame checks object content
Assuming assertSame works for null references only
2. Which of the following is the correct syntax to assert that two objects are NOT the same in JUnit?
easy
A. assertNotSame(object1, object2);
B. assertNotSame(object1 == object2);
C. assertNotSame(object1.equals(object2));
D. assertNotSame(object1 != object2);
Solution
Step 1: Recall the method signature of assertNotSame
The correct syntax is assertNotSame(expected, actual), both are objects to compare references.
Step 2: Identify incorrect usage
Options A, C, and D misuse assertNotSame by passing boolean expressions instead of objects.
Final Answer:
assertNotSame(object1, object2); -> Option A
Quick Check:
assertNotSame takes two objects, not booleans [OK]
Hint: Use assertNotSame(obj1, obj2) with two objects, not booleans [OK]
Common Mistakes:
Passing boolean expressions instead of objects
Using assertNotSame with equals() method
Confusing assertNotSame syntax with assertEquals
3. Consider the following code snippet:
String a = "test";
String b = "test";
String c = new String("test");
assertSame(a, b);
assertNotSame(a, c);
What will be the result of running these assertions?
medium
A. First assertion passes, second fails
B. First assertion fails, second passes
C. Both assertions pass
D. Both assertions fail
Solution
Step 1: Analyze references of a and b
Both a and b point to the same string literal in the string pool, so assertSame(a, b) passes.
Step 2: Analyze reference of c
c is a new String object created on the heap, different from a, so assertNotSame(a, c) passes.
Final Answer:
Both assertions pass -> Option C
Quick Check:
String literals share references, new String creates new object [OK]
Hint: String literals share references; new String creates new object [OK]
C. obj1 and obj2 are different objects, so test should pass
D. obj1 and obj2 refer to the same object, so assertNotSame fails
Solution
Step 1: Check references of obj1 and obj2
Both variables point to the same object because obj2 = obj1 assigns the reference.
Step 2: Understand assertNotSame behavior
assertNotSame expects different objects; since references are same, assertion fails.
Final Answer:
obj1 and obj2 refer to the same object, so assertNotSame fails -> Option D
Quick Check:
Same reference fails assertNotSame [OK]
Hint: assertNotSame fails if references point to same object [OK]
Common Mistakes:
Assuming assertNotSame checks content equality
Thinking assertNotSame syntax is wrong
Believing assertNotSame cannot test Object instances
5. Given the following code:
class Person {
String name;
Person(String name) { this.name = name; }
}
Person p1 = new Person("Alice");
Person p2 = new Person("Alice");
Person p3 = p1;
assertSame(p1, p3);
assertNotSame(p1, p2);
Which statement about these assertions is true?
hard
A. assertSame(p1, p3) fails because objects have same content but different references; assertNotSame(p1, p2) passes
B. assertSame(p1, p3) passes because p1 and p3 reference the same object; assertNotSame(p1, p2) passes because p1 and p2 are different objects even if names match
C. assertSame(p1, p3) passes; assertNotSame(p1, p2) fails because names are equal
D. Both assertions fail because Person objects are not immutable
Solution
Step 1: Analyze references of p1 and p3
p3 is assigned from p1, so both point to the same object; assertSame(p1, p3) passes.
Step 2: Analyze references of p1 and p2
p2 is a new Person object with same name but different reference; assertNotSame(p1, p2) passes.
Final Answer:
assertSame(p1, p3) passes; assertNotSame(p1, p2) passes because references differ -> Option B
Quick Check:
assertSame checks references, not content [OK]
Hint: assertSame checks references; assertNotSame passes if objects differ [OK]
Common Mistakes:
Confusing object content equality with reference equality
Assuming assertNotSame fails if object fields match
Thinking assertSame fails if objects are mutable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.