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 when a full mock is needed.
Using verify to create objects, which is for checking calls.
✗ Incorrect
The mock method creates a mock object for the given class.
2fill in blank
mediumComplete the code to verify that a method was called once on a mock.
JUnit
Mockito.[1](mockObject).someMethod(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mock() instead of verify() for checking calls.
Confusing spy with verify.
✗ Incorrect
The verify method checks if a method was called on a mock.
3fill in blank
hardFix the error in the code to stub a method call on a spy object.
JUnit
Mockito.[1](spyObject.someMethod()).thenReturn("value");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using verify instead of when for stubbing.
Using mock instead of when for stubbing.
✗ Incorrect
The when method is used to stub method calls on mocks or spies.
4fill in blank
hardFill both blanks to create a spy and stub a method to return a value.
JUnit
MyService spyService = Mockito.[1](new MyService()); Mockito.[2](spyService.someMethod()).thenReturn("Hello");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mock instead of spy to wrap a real object.
Using verify instead of when for stubbing.
✗ Incorrect
First, create a spy with spy(). Then stub a method call with when().
5fill in blank
hardFill all three blanks to mock a repository, stub a method, and verify the call.
JUnit
Repository repo = Mockito.[1](Repository.class); Mockito.[2](repo.findById(1)).thenReturn(Optional.of(new Item())); Mockito.[3](repo).findById(1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing spy and mock incorrectly.
Using verify before when.
✗ Incorrect
Create a mock with mock(), stub a method with when(), and verify the call with verify().