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 partial mock, not a full mock.
Using 'verify' or 'when' does not create mocks.
✗ Incorrect
Mockito.mock() creates a mock object for the given class.
2fill in blank
mediumComplete the code to stub a method call to return a specific value.
JUnit
Mockito.[1](service.getData()).thenReturn("mocked data");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'verify' instead of 'when' is for checking calls, not stubbing.
Using 'mock' or 'spy' here is incorrect.
✗ Incorrect
Mockito.when() is used to stub a method call to return a desired value.
3fill in blank
hardFix the error in verifying that a method was called exactly once.
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()' causes a compilation error because it is not a valid Mockito method.
Using 'times(2)' checks for two calls, not one.
✗ Incorrect
Mockito.times(1) verifies the method was called exactly once. 'once()' does not exist.
4fill in blank
hardFill both blanks to mock a complex dependency and stub its method.
JUnit
Dependency dep = Mockito.[1](Dependency.class); Mockito.[2](dep.getValue()).thenReturn(42);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spy' instead of 'mock' creates a partial mock, which may not be desired.
Using 'verify' instead of 'when' is incorrect for stubbing.
✗ Incorrect
First, create a mock with mock(). Then stub the method with when().
5fill in blank
hardFill all three blanks to verify a method call with argument matcher and times.
JUnit
Mockito.verify(service, Mockito.[1](3)).execute(Mockito.[2]("test"), Mockito.[3]());
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'never' instead of 'times' causes verification to expect zero calls.
Using 'any' instead of 'eq' loses exact argument matching.
✗ Incorrect
Use times(3) to check call count, eq("test") to match exact argument, and any() for any argument.