0
0
JUnittesting~10 mins

Why mocking isolates units under test in JUnit - Test Your Understanding

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
UserService userService = Mockito.[1](UserService.class);
Drag options to blanks, or click blank then click option'
Averify
Bspy
Cmock
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spy' instead of 'mock' creates a partial mock, not a full mock.
Using 'verify' or 'when' are for other purposes, not creating mocks.
2fill in blank
medium

Complete the code to stub a method call on the mock to return a specific value.

JUnit
Mockito.[1](userService.getUserName()).thenReturn("Alice");
Drag options to blanks, or click blank then click option'
Averify
Bwhen
Cmock
Dspy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'verify' instead of 'when' confuses verification with stubbing.
Using 'mock' or 'spy' here is incorrect as they create or wrap objects.
3fill in blank
hard

Fix the error in the verification statement to check if getUserName() was called once.

JUnit
Mockito.verify(userService, Mockito.[1]()).getUserName();
Drag options to blanks, or click blank then click option'
Atimes(1)
BatLeastOnce()
Cnever()
Dtimes(2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using times(2) checks for two calls, which is incorrect here.
Using never() checks that the method was not called, which is wrong.
Using atLeastOnce() allows multiple calls, not exactly one.
4fill in blank
hard

Fill both blanks to stub a method and verify it was called with a specific argument.

JUnit
Mockito.[1](userService.getUserById([2])).thenReturn(new User("Bob"));
Mockito.verify(userService).getUserById(5);
Drag options to blanks, or click blank then click option'
Awhen
Bverify
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using verify() in the first blank is wrong because it is for checking calls, not stubbing.
Using 10 instead of 5 causes verification to fail.
5fill in blank
hard

Fill all three blanks to create a mock, stub a method, and verify the call.

JUnit
UserRepository repo = Mockito.[1](UserRepository.class);
Mockito.[2](repo.findUserByEmail([3])).thenReturn(new User("Eve"));
Mockito.verify(repo).findUserByEmail("eve@example.com");
Drag options to blanks, or click blank then click option'
Amock
Bwhen
C"eve@example.com"
Dspy
Attempts:
3 left
💡 Hint
Common Mistakes
Using spy() instead of mock() creates a partial mock, not a full mock.
Not passing the correct email string causes verification to fail.