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?✗ Incorrect
assertSame checks if both references point to the same object in memory.Which assertion would you use to check that two variables do NOT refer to the same object?
✗ Incorrect
assertNotSame verifies that two references are not the same object.If
assertEquals(obj1, obj2) passes but assertSame(obj1, obj2) fails, what does it mean?✗ Incorrect
assertEquals checks content equality; assertSame checks object identity.Which of these is a correct use of
assertNotSame?✗ Incorrect
Two different String objects with same content are not the same object, so
assertNotSame passes.What will happen if
assertSame(obj1, obj2) is used and obj1 is null but obj2 is not?✗ Incorrect
assertSame fails if references are not the same, including if one is null and the other is not.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.