Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
Mockito.mock() creates a mock object of the given class.
2fill in blank
mediumComplete 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'
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().
✗ Incorrect
The method 'getData' is stubbed to return 'Hello' when called.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once()' instead of 'times(1)'.
Using 'calledOnce()' or 'verifyOnce()' which are invalid.
✗ Incorrect
Mockito.times(1) verifies the method was called exactly once.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like 'compute'.
Using return value as argument.
✗ Incorrect
The method 'calculate' is stubbed to return 10 when called with argument 5.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once' instead of 'times' with 2.
Mixing method names or call counts.
✗ Incorrect
Mockito.verify with times(2) checks 'update' was called twice with argument 'data'.