0
0
JUnittesting~10 mins

@Spy for partial mocking 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 object for partial mocking.

JUnit
List<String> list = Mockito.[1](new ArrayList<>());
Drag options to blanks, or click blank then click option'
Amock
Bverify
Cspy
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using mock() instead of spy() causes full mocking, not partial.
Using verify() or when() here is incorrect as they are for verification and stubbing.
2fill in blank
medium

Complete the code to stub a method on a spy object.

JUnit
Mockito.[1](spyList.size()).thenReturn(5);
Drag options to blanks, or click blank then click option'
Awhen
BdoReturn
Cverify
Dspy
Attempts:
3 left
💡 Hint
Common Mistakes
Using doReturn() without when() can cause confusion in spies.
Using verify() is for checking calls, not stubbing.
3fill in blank
hard

Fix the error in stubbing a spy method to avoid calling the real method.

JUnit
Mockito.[1](10).when(spyList).size();
Drag options to blanks, or click blank then click option'
Awhen
Bspy
Cverify
DdoReturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using when() on spies calls the real method, which may cause errors.
Using verify() does not stub methods.
4fill in blank
hard

Fill both blanks to stub a spy method without calling the real method.

JUnit
Mockito.[1](10).when(spyList).[2]();
Drag options to blanks, or click blank then click option'
AdoReturn
Bwhen
Csize
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using when() instead of doReturn() causes the real method to be called.
Using clear() instead of size() changes the method being stubbed.
5fill in blank
hard

Fill both blanks to verify a method call on a spy with a specific argument.

JUnit
Mockito.[1](spyList).[2]("test");
Drag options to blanks, or click blank then click option'
Averify
Badd
Cremove
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using doReturn() or when() instead of verify() for checking calls.
Using remove() or clear() instead of add() changes the method verified.