Challenge - 5 Problems
Mockito Stubbing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of when().thenReturn() stubbed method call
Given the following JUnit test code using Mockito, what will be the output of the test assertion?
JUnit
import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; interface Calculator { int add(int a, int b); } public class CalculatorTest { @Test void testAdd() { Calculator calc = mock(Calculator.class); when(calc.add(2, 3)).thenReturn(10); int result = calc.add(2, 3); assertEquals(10, result); } }
Attempts:
2 left
💡 Hint
Remember that when().thenReturn() sets the return value for the mocked method call.
✗ Incorrect
The stub sets add(2, 3) to return 10, so the assertion assertEquals(10, result) passes.
❓ assertion
intermediate2:00remaining
Correct assertion for stubbed method return
You stub a method call with when().thenReturn() to return "Hello". Which assertion correctly verifies this behavior?
JUnit
import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; interface Greeter { String greet(); } public class GreeterTest { @Test void testGreet() { Greeter greeter = mock(Greeter.class); when(greeter.greet()).thenReturn("Hello"); String message = greeter.greet(); // Which assertion is correct here? } }
Attempts:
2 left
💡 Hint
Use assertEquals to compare string values in tests.
✗ Incorrect
assertEquals("Hello", message) correctly checks that the stubbed method returns "Hello".
🔧 Debug
advanced2:00remaining
Identify the error in stubbing with when().thenReturn()
What is wrong with the following stubbing code snippet?
JUnit
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.List; List<String> list = mock(List.class); when(list.get(0)).thenReturn("first"); when(list.get(anyInt())).thenReturn("any"); String result = list.get(0);
Attempts:
2 left
💡 Hint
Consider the order and specificity of stubbing calls.
✗ Incorrect
The second stub with anyInt() matches all indices including 0, overriding the first stub. So list.get(0) returns "any".
🧠 Conceptual
advanced2:00remaining
Behavior of unstubbed method calls on mocks
If you create a mock object and stub only one method with when().thenReturn(), what happens when you call another method on the mock that was not stubbed?
Attempts:
2 left
💡 Hint
Think about how Mockito handles unstubbed method calls by default.
✗ Incorrect
By default, unstubbed methods on mocks return default values: null for objects, 0 for numbers, false for booleans.
❓ framework
expert3:00remaining
Correct usage of when().thenReturn() with chained method calls
Given a mock object with a method that returns another mock, how do you correctly stub a chained method call using when().thenReturn()?
JUnit
interface Service {
Repository getRepository();
}
interface Repository {
String findName();
}
Service service = mock(Service.class);
Repository repo = mock(Repository.class);
// How to stub service.getRepository().findName() to return "Data"?Attempts:
2 left
💡 Hint
You must stub each method in the chain separately if the intermediate method returns a mock.
✗ Incorrect
You first stub service.getRepository() to return the mock repo, then stub repo.findName() to return "Data".