0
0
JUnittesting~10 mins

Mock 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 mock object using Mockito.

JUnit
MyService service = Mockito.[1](MyService.class);
Drag options to blanks, or click blank then click option'
Anew
Bspy
Ccreate
Dmock
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spy' instead of 'mock' creates a spy, not a pure mock.
Using 'new' is not a Mockito method.
2fill in blank
medium

Complete the code to stub the method 'getData' to return 'Hello' when called.

JUnit
Mockito.when(service.[1]()).thenReturn("Hello");
Drag options to blanks, or click blank then click option'
AfetchData
BgetData
Cretrieve
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the service interface.
Forgetting to use Mockito.when() before thenReturn().
3fill in blank
hard

Fix the error in verifying that 'process' method was called once on the mock.

JUnit
Mockito.verify(service, Mockito.[1]()).process();
Drag options to blanks, or click blank then click option'
AcalledOnce()
Bonce()
Ctimes(1)
DverifyOnce()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once()' instead of 'times(1)'.
Using 'calledOnce()' or 'verifyOnce()' which are invalid.
4fill in blank
hard

Fill both blanks to stub 'calculate' method to return 10 when called with argument 5.

JUnit
Mockito.when(service.[1]([2])).thenReturn(10);
Drag options to blanks, or click blank then click option'
Acalculate
B5
C10
Dcompute
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like 'compute'.
Using return value as argument.
5fill in blank
hard

Fill all three blanks to verify 'update' method was called twice with argument 'data'.

JUnit
Mockito.verify(service, Mockito.[1]([2])).[3]("data");
Drag options to blanks, or click blank then click option'
Atimes
B2
Cupdate
Donce
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once' instead of 'times' with 2.
Mixing method names or call counts.