Challenge - 5 Problems
JUnit assertSame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of assertSame with identical objects
What will be the result of this JUnit test method execution?
JUnit
String a = "test";
String b = a;
assertSame(a, b);Attempts:
2 left
💡 Hint
assertSame checks if both references point to the exact same object.
✗ Incorrect
Since both variables 'a' and 'b' refer to the same String object, assertSame passes.
❓ Predict Output
intermediate2:00remaining
Result of assertNotSame with different objects
What happens when this JUnit assertion runs?
JUnit
String a = new String("hello"); String b = new String("hello"); assertNotSame(a, b);
Attempts:
2 left
💡 Hint
assertNotSame checks that two references do not point to the same object.
✗ Incorrect
Though the strings have the same content, 'a' and 'b' are different objects, so assertNotSame passes.
❓ assertion
advanced2:00remaining
Which assertion fails here?
Given these variables, which assertion will fail when executed?
JUnit
Integer x = 128; Integer y = 128; assertSame(x, y); assertNotSame(x, y);
Attempts:
2 left
💡 Hint
Integer caching affects small values but not 128.
✗ Incorrect
For Integer 128, 'x' and 'y' are different objects, so assertSame fails but assertNotSame passes.
🧠 Conceptual
advanced2:00remaining
Purpose of assertSame vs assertEquals
Which statement correctly explains the difference between assertSame and assertEquals in JUnit?
Attempts:
2 left
💡 Hint
Think about identity vs equality.
✗ Incorrect
assertSame tests if two variables refer to the exact same object instance; assertEquals tests if two objects are equal in value.
🔧 Debug
expert3:00remaining
Why does this assertSame fail unexpectedly?
Consider this test code snippet:
String a = "abc";
String s1 = a + "def";
String s2 = "abcdef";
assertSame(s1, s2);
Why does assertSame fail here?
Attempts:
2 left
💡 Hint
Consider when Java creates String objects and how concatenation works.
✗ Incorrect
The expression a + "def" is evaluated at runtime creating a new String object, so s1 and s2 reference different objects.