Hint: Use mock(Class.class) to create mocks in Mockito [OK]
Common Mistakes:
Using new Mock<>() which is not valid
Calling mock() on an instance
Using createMock which is not Mockito syntax
3. Given the following code snippet, what will result be after execution?
List<String> mockList = mock(List.class);
when(mockList.size()).thenReturn(5);
int result = mockList.size();
medium
A. 5
B. 0
C. Throws NullPointerException
D. Returns the actual size of a real list
Solution
Step 1: Understand the mock setup
The mock list's size() method is stubbed to return 5 when called.
Step 2: Evaluate the method call
Calling mockList.size() returns 5 as per the stub, not the real list size.
Final Answer:
5 -> Option A
Quick Check:
Stubbed method returns set value 5 [OK]
Hint: Stubbed methods return specified values, not defaults [OK]
Common Mistakes:
Expecting default 0 for int return
Thinking mock calls throw exceptions by default
Confusing mock with real object behavior
4. Identify the error in the following JUnit test code using Mockito:
MyService service = mock(MyService.class);
when(service.getData()).thenReturn("data");
verify(service).getData();
medium
A. No error, code is correct
B. Incorrect use of mock() method
C. Cannot verify void methods
D. Missing call to service.getData() before verify
Solution
Step 1: Understand verify usage in Mockito
Verification checks if a method was called on the mock during the test.
Step 2: Check if method was called before verify
The code never calls service.getData() before verify, so verification fails.
Final Answer:
Missing call to service.getData() before verify -> Option D
Quick Check:
Verify requires method call before checking [OK]
Hint: Always call method before verify to avoid errors [OK]
Common Mistakes:
Verifying without calling the method
Confusing mock creation syntax
Thinking verify only works on void methods
5. You want to test a method that calls a dependency twice with different inputs and expects different outputs. How do you configure a mock to return "first" on the first call and "second" on the second call to getValue()?
hard
A. when(mock.getValue()).thenReturn("first"); when(mock.getValue()).thenReturn("second");
B. when(mock.getValue()).thenReturn(new String[]{"first", "second"});
C. when(mock.getValue()).thenReturn("first").thenReturn("second");
D. when(mock.getValue()).thenReturn("second").thenReturn("first");
Solution
Step 1: Recall Mockito chaining for multiple returns
Mockito allows chaining thenReturn() calls to specify sequential return values.
Step 2: Match chaining syntax to options
when(mock.getValue()).thenReturn("first").thenReturn("second"); matches the correct chaining syntax for sequential calls.
Final Answer:
when(mock.getValue()).thenReturn("first").thenReturn("second"); -> Option C
Quick Check:
Chain thenReturn() for multiple call returns [OK]
Hint: Chain thenReturn() calls for sequential mock returns [OK]
Common Mistakes:
Passing multiple values in one thenReturn() call
Overwriting stubs instead of chaining
Reversing the order of return values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.