0
0
JUnittesting~10 mins

assertSame and assertNotSame in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if two references point to the same object using JUnit.

JUnit
assert[1](obj1, obj2);
Drag options to blanks, or click blank then click option'
AassertSame
BassertNotNull
CassertEquals
DassertTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertEquals instead of assertSame, which checks value equality, not reference equality.
2fill in blank
medium

Complete the code to verify that two references do NOT point to the same object using JUnit.

JUnit
assert[1](objA, objB);
Drag options to blanks, or click blank then click option'
AassertNotSame
BassertFalse
CassertNull
DassertEquals
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertEquals or assertFalse which do not check reference identity.
3fill in blank
hard

Fix the error in the assertion that checks if two objects are the same instance.

JUnit
assertSame([1], obj);
Drag options to blanks, or click blank then click option'
Aobj.hashCode()
Bobj.equals(obj2)
Cobj1
Dobj.toString()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing method calls like obj.equals() or obj.toString() instead of object references.
4fill in blank
hard

Fill both blanks to assert that two variables do NOT refer to the same object and provide a failure message.

JUnit
assert[1]("Objects should not be same", [2], obj2);
Drag options to blanks, or click blank then click option'
AassertNotSame
BassertSame
Cobj1
Dobj3
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertSame instead of assertNotSame.
Passing the wrong object reference.
5fill in blank
hard

Fill all three blanks to assert that two objects are the same instance with a custom failure message.

JUnit
assert[1]("Failure: objects differ", [2], [3]);
Drag options to blanks, or click blank then click option'
AassertEquals
BobjA
CobjB
DassertSame
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertEquals instead of assertSame.
Swapping the order of object references.