0
0
JUnittesting~20 mins

when().thenReturn() stubbing in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mockito Stubbing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
ATest fails because the stub returns 5 for add(2, 3)
BTest passes because the stub returns 10 for add(2, 3)
CTest fails with NullPointerException because add is not implemented
DTest fails because the stub returns 0 for add(2, 3)
Attempts:
2 left
💡 Hint
Remember that when().thenReturn() sets the return value for the mocked method call.
assertion
intermediate
2: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?
    }
}
AassertEquals("Hello", message);
BassertTrue(message == "Hello");
CassertNull(message);
DassertFalse(message.equals("Hello"));
Attempts:
2 left
💡 Hint
Use assertEquals to compare string values in tests.
🔧 Debug
advanced
2: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);
AThe code returns null because mocks do not support stubbing
BThe code throws a NullPointerException because of anyInt() usage
CThe second when() overrides the first, so list.get(0) returns "any" instead of "first"
DThe code causes a compile error because thenReturn() is missing a semicolon
Attempts:
2 left
💡 Hint
Consider the order and specificity of stubbing calls.
🧠 Conceptual
advanced
2: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?
AThe unstubbed method returns the default value for its return type (e.g., null, 0, false)
BThe unstubbed method throws an exception immediately
CThe unstubbed method returns the last stubbed value
DThe unstubbed method causes the test to fail automatically
Attempts:
2 left
💡 Hint
Think about how Mockito handles unstubbed method calls by default.
framework
expert
3: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"?
Awhen(service.getRepository().findName()).thenReturn("Data");
B
when(service.getRepository()).thenReturn(null);
when(repo.findName()).thenReturn("Data");
C
when(service.getRepository().findName()).thenReturn(null);
when(repo.findName()).thenReturn("Data");
D
when(service.getRepository()).thenReturn(repo);
when(repo.findName()).thenReturn("Data");
Attempts:
2 left
💡 Hint
You must stub each method in the chain separately if the intermediate method returns a mock.