0
0
JUnittesting~5 mins

assertSame and assertNotSame in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does assertSame check in JUnit?
assertSame checks if two references point to the exact same object in memory.
Click to reveal answer
beginner
What is the difference between assertSame and assertEquals?
assertSame checks if two references are identical (same object), while assertEquals checks if two objects are logically equal (same content).
Click to reveal answer
beginner
What does assertNotSame verify?
assertNotSame verifies that two references do NOT point to the same object in memory.
Click to reveal answer
beginner
Give a simple example of using assertSame in a test.
Example:<br>
String a = "hello";<br>String b = a;<br>assertSame(a, b); // Passes because both refer to the same object
Click to reveal answer
intermediate
Why might assertSame fail even if two objects look equal?
Because assertSame checks object identity, not content. Two different objects with the same content will fail assertSame.
Click to reveal answer
What does assertSame(obj1, obj2) verify in JUnit?
Aobj1 and obj2 are the exact same object
Bobj1 and obj2 have the same content
Cobj1 is not null
Dobj1 and obj2 are different objects
Which assertion would you use to check that two variables do NOT refer to the same object?
AassertSame
BassertEquals
CassertNotSame
DassertNull
If assertEquals(obj1, obj2) passes but assertSame(obj1, obj2) fails, what does it mean?
Aobj1 and obj2 are the same object
BTest is invalid
Cobj1 is null
Dobj1 and obj2 have equal content but are different objects
Which of these is a correct use of assertNotSame?
AassertNotSame(new String("a"), new String("a"))
BassertNotSame("a", "a")
CassertNotSame(null, null)
DassertNotSame(obj, obj)
What will happen if assertSame(obj1, obj2) is used and obj1 is null but obj2 is not?
ATest passes
BTest fails
CThrows exception
DTest is skipped
Explain in your own words the difference between assertSame and assertEquals.
Think about whether the test checks if two things are literally the same object or just look the same.
You got /4 concepts.
    Describe a situation where using assertNotSame is important in testing.
    Consider cases where sharing the same object could cause bugs.
    You got /4 concepts.