0
0
JUnittesting~20 mins

doReturn and doThrow in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mockito Mastery: doReturn and doThrow
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of doReturn with Mockito
What will be the output of the following JUnit test using Mockito's doReturn?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class Service {
    String getData() { return "real"; }
}

public class ServiceTest {
    @Test
    void testDoReturn() {
        Service mockService = mock(Service.class);
        doReturn("mocked").when(mockService).getData();
        String result = mockService.getData();
        assertEquals("mocked", result);
        System.out.println(result);
    }
}
AThrows NullPointerException
Breal
Cnull
Dmocked
Attempts:
2 left
💡 Hint
doReturn sets the return value for the mocked method call.
Predict Output
intermediate
2:00remaining
Effect of doThrow in Mockito
What happens when the following JUnit test runs with Mockito's doThrow?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class Service {
    void perform() {}
}

public class ServiceTest {
    @Test
    void testDoThrow() {
        Service mockService = mock(Service.class);
        doThrow(new IllegalStateException("fail")).when(mockService).perform();
        assertThrows(IllegalStateException.class, () -> mockService.perform());
    }
}
ATest passes because perform() throws IllegalStateException
BTest fails because perform() returns normally
CTest fails with NullPointerException
DTest fails with AssertionError due to wrong exception
Attempts:
2 left
💡 Hint
doThrow makes the mocked method throw the specified exception.
assertion
advanced
2:30remaining
Correct assertion for doThrow exception message
Which assertion correctly verifies that a mocked method throws an exception with message "error occurred" using doThrow?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class Service {
    void execute() {}
}

public class ServiceTest {
    @Test
    void testExceptionMessage() {
        Service mockService = mock(Service.class);
        doThrow(new RuntimeException("error occurred")).when(mockService).execute();
        // Which assertion below is correct?
    }
}
AassertTrue(() -> mockService.execute().equals("error occurred"));
BRuntimeException ex = assertThrows(RuntimeException.class, () -> mockService.execute()); assertEquals("error occurred", ex.getMessage());
CassertThrows(RuntimeException.class, () -> mockService.execute(), "error occurred");
DassertThrowsMessage(RuntimeException.class, "error occurred", () -> mockService.execute());
Attempts:
2 left
💡 Hint
Use assertThrows to capture the exception and then check its message.
🔧 Debug
advanced
2:30remaining
Why does doReturn fail with spy on final method?
Given a spy on a class with a final method, why does the following code throw an exception?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

class FinalClass {
    final String finalMethod() { return "real"; }
}

public class SpyTest {
    @Test
    void testDoReturnOnFinal() {
        FinalClass spy = spy(new FinalClass());
        doReturn("mocked").when(spy).finalMethod();
        String result = spy.finalMethod();
    }
}
AThe spy object is null, causing NullPointerException.
BdoReturn requires the method to be static, finalMethod is not static.
CMockito cannot mock or stub final methods by default, so doReturn fails.
DdoReturn only works with interfaces, not classes.
Attempts:
2 left
💡 Hint
Mockito has limitations with final methods unless configured.
🧠 Conceptual
expert
3:00remaining
Difference between doReturn().when() and when().thenReturn()
Which statement correctly explains the difference between doReturn().when() and when().thenReturn() in Mockito?
A<code>doReturn().when()</code> can stub methods on spies without calling the real method, while <code>when().thenReturn()</code> calls the real method on spies during stubbing.
B<code>when().thenReturn()</code> is used only for void methods, <code>doReturn().when()</code> is for non-void methods.
C<code>doReturn().when()</code> is deprecated and should not be used; <code>when().thenReturn()</code> is the modern replacement.
DBoth behave identically in all cases; the difference is only stylistic.
Attempts:
2 left
💡 Hint
Consider how spies behave when stubbing methods.