Discover how a simple check can save hours of confusion and catch hidden bugs!
Why assertSame and assertNotSame in JUnit? - Purpose & Use Cases
Imagine you are checking if two boxes contain the exact same toy by opening them and comparing each toy piece manually.
This manual checking is slow and confusing because toys might look the same but be different pieces. You might miss subtle differences or waste time comparing things that are actually different objects.
Using assertSame and assertNotSame in tests lets you quickly check if two variables point to the exact same object or not, without guessing or comparing details manually.
if (obj1 == obj2) { System.out.println("Same object"); } else { System.out.println("Different objects"); }
assertSame(obj1, obj2); assertNotSame(obj1, obj3);
It enables fast and precise verification of object identity, catching subtle bugs that normal equality checks might miss.
When testing a cache system, you want to confirm that the same cached object is returned each time, not just an equal copy. assertSame helps verify this behavior.
assertSame checks if two references point to the exact same object.
assertNotSame confirms two references are different objects.
These assertions save time and avoid errors compared to manual identity checks.
Practice
assertSame check in a JUnit test?Solution
Step 1: Understand the purpose of assertSame
assertSamechecks if both references point to the exact same object in memory, not just equal content.Step 2: Differentiate from equality checks
Other assertions likeassertEqualscheck if objects are equal in content, butassertSamechecks identity.Final Answer:
If two references point to the exact same object -> Option AQuick Check:
assertSame = same object reference [OK]
- Confusing assertSame with assertEquals
- Thinking assertSame checks object content
- Assuming assertSame works for null references only
Solution
Step 1: Recall the method signature of assertNotSame
The correct syntax isassertNotSame(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 AQuick Check:
assertNotSame takes two objects, not booleans [OK]
- Passing boolean expressions instead of objects
- Using assertNotSame with equals() method
- Confusing assertNotSame syntax with assertEquals
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?
Solution
Step 1: Analyze references of a and b
Bothaandbpoint to the same string literal in the string pool, soassertSame(a, b)passes.Step 2: Analyze reference of c
cis a new String object created on the heap, different froma, soassertNotSame(a, c)passes.Final Answer:
Both assertions pass -> Option CQuick Check:
String literals share references, new String creates new object [OK]
- Assuming new String shares reference with literal
- Confusing assertSame with assertEquals
- Thinking assertNotSame fails if content equals
Object obj1 = new Object(); Object obj2 = obj1; assertNotSame(obj1, obj2);
The test fails. What is the problem?
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
assertNotSameexpects different objects; since references are same, assertion fails.Final Answer:
obj1 and obj2 refer to the same object, so assertNotSame fails -> Option DQuick Check:
Same reference fails assertNotSame [OK]
- Assuming assertNotSame checks content equality
- Thinking assertNotSame syntax is wrong
- Believing assertNotSame cannot test Object instances
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?
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 BQuick Check:
assertSame checks references, not content [OK]
- Confusing object content equality with reference equality
- Assuming assertNotSame fails if object fields match
- Thinking assertSame fails if objects are mutable
Ready to prove it?
Pass the test and this pattern moves unknown
