0
0
JUnittesting~10 mins

Spy objects 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 create a spy of the List class using Mockito.

JUnit
List<String> spyList = Mockito.[1](new ArrayList<>());
Drag options to blanks, or click blank then click option'
Aspy
Bmock
Cverify
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using mock() instead of spy() will create a mock without real method calls.
Using verify() or when() here is incorrect because they are for verification and stubbing.
2fill in blank
medium

Complete the code to verify that the add method was called once on the spyList.

JUnit
Mockito.[1](spyList).add("item");
Drag options to blanks, or click blank then click option'
Awhen
Bspy
Cverify
Dmock
Attempts:
3 left
💡 Hint
Common Mistakes
Using when() instead of verify() confuses stubbing with verification.
Using spy() or mock() here is incorrect because they create objects, not verify calls.
3fill in blank
hard

Fix the error in stubbing the size method of spyList to return 5.

JUnit
Mockito.[1](5).when(spyList).size();
Drag options to blanks, or click blank then click option'
Awhen
Bverify
Cspy
DdoReturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using when(spyList.size()) calls the real method immediately, which can cause errors.
Using verify() or spy() here is incorrect for stubbing.
4fill in blank
hard

Fill both blanks to stub the get method of spyList to return "mocked" for index 0.

JUnit
Mockito.[1]("mocked").when(spyList).[2](0);
Drag options to blanks, or click blank then click option'
AdoReturn
Bwhen
Cget
Dspy
Attempts:
3 left
💡 Hint
Common Mistakes
Using when() in the first blank causes the real method to be called during stubbing.
Using spy() or verify() in blanks is incorrect here.
5fill in blank
hard

Fill all three blanks to verify that the clear method was called exactly twice on spyList.

JUnit
Mockito.[1](spyList, Mockito.[2](2)).[3]();
Drag options to blanks, or click blank then click option'
Averify
Btimes
Cclear
Dspy
Attempts:
3 left
💡 Hint
Common Mistakes
Using spy() in the first blank instead of verify().
Using times() incorrectly or omitting it causes wrong verification.
Using wrong method name in the third blank.